Extract part of file path (directory) in Python

I need to extract the parent directory name of a specific path. It looks like this: c:\ stuff \ directory_i_need \ subdir \ file . I modify the contents of the β€œfile” by using the name directory_i_need (and not the path) in it. I created a function that will give me a list of all files, and then ...

 for path in file_list: #directory_name = os.path.dirname(path) # this is not what I need, that why it is commented directories, files = path.split('\\') line_replace_add_directory = line_replace + directories # this is what I want to add in the text, with the directory name at the end # of the line. 

How can i do this?

+69
python directory filepath
Apr 13 2018-12-12T00:
source share
5 answers
 import os ## first file in current dir (with full path) file = os.path.join(os.getcwd(), os.listdir(os.getcwd())[0]) file os.path.dirname(file) ## directory of file os.path.dirname(os.path.dirname(file)) ## directory of directory of file ... 

And you can continue to do this as many times as necessary ...

Edit: from os.path , you can use either os.path.split or os.path. base name:

 dir = os.path.dirname(os.path.dirname(file)) ## dir of dir of file ## once you're at the directory level you want, with the desired directory as the final path node: dirname1 = os.path.basename(dir) dirname2 = os.path.split(dir)[1] ## if you look at the documentation, this is exactly what os.path.basename does. 
+100
Apr 13 '12 at 23:01
source share

In Python 3.4, you can use the pathlib module :

 >>> from pathlib import Path >>> p = Path('C:\Program Files\Internet Explorer\iexplore.exe') >>> p.name 'iexplore.exe' >>> p.suffix '.exe' >>> p.root '\\' >>> p.parts ('C:\\', 'Program Files', 'Internet Explorer', 'iexplore.exe') >>> p.relative_to('C:\Program Files') WindowsPath('Internet Explorer/iexplore.exe') >>> p.exists() True 
+12
Jan 19 '16 at 10:37
source share

First, see if there is splitunc() as an available function in os.path . The first returned item should be what you want ... but I'm on Linux and I don't have this function when I import os and try to use it.

Otherwise, one semi-fun way to do this work is to use:

 >>> pathname = "\\C:\\mystuff\\project\\file.py" >>> pathname '\\C:\\mystuff\\project\\file.py' >>> print pathname \C:\mystuff\project\file.py >>> "\\".join(pathname.split('\\')[:-2]) '\\C:\\mystuff' >>> "\\".join(pathname.split('\\')[:-1]) '\\C:\\mystuff\\project' 

which shows extracting the directory directly above the file and the directory just above it.

+4
Apr 13 '12 at 23:00
source share

Here is what I did to extract the directory fragment:

 for path in file_list: directories = path.rsplit('\\') directories.reverse() line_replace_add_directory = line_replace+directories[2] 

Thank you for your help.

+1
Apr 16 '12 at 17:14
source share

You must put the whole path as a parameter in os.path.split. See Documents . This does not work like line splitting.

-one
Apr 13 2018-12-12T00:
source share



All Articles