Extract a part of the filepath (a directory) in Python
Asked Answered
S

7

258

I need to extract the name of the parent directory of a certain path. This is what it looks like:

C:\stuff\directory_i_need\subdir\file.jpg

I would like to extract directory_i_need.

Stroke answered 13/4, 2012 at 22:48 Comment(3)
You might want to check this answer out: https://mcmap.net/q/46450/-cross-platform-splitting-of-path-in-pythonBayard
The above link helped me understand how to fix what I did wrong. Thank you.Stroke
or this one: https://mcmap.net/q/46450/-cross-platform-splitting-of-path-in-pythonIntrude
P
330
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 doing this as many times as necessary...

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

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.
Plated answered 13/4, 2012 at 23:1 Comment(3)
It does extract parts of the path - but I don't know how to extract the actual directory name from the path.Stroke
+1: A scenario where os.path.dirname(path) is handy compared to pathlib.Path(path).parent: You are given a string path. You want to create the directory of path if it does not exist, whether path itself is a directory or not. For example, path could be /home/me/directory_to_create/file_to_create.txt or /home/me/directory_to_create/. In the second case, pathlib.Path(path).parent returns /home/me/ which is not desired.Magee
I wouldn't recommend copying the second snippet verbatim, dir() is a reserved keyword in Python now: docs.python.org/3/library/functions.html#dirAltorelievo
P
106

For Python 3.4+, try the pathlib module:

>>> from pathlib import Path

>>> p = Path('C:\\Program Files\\Internet Explorer\\iexplore.exe')

>>> str(p.parent)
'C:\\Program Files\\Internet Explorer'

>>> p.name
'iexplore.exe'

>>> p.suffix
'.exe'

>>> p.parts
('C:\\', 'Program Files', 'Internet Explorer', 'iexplore.exe')

>>> p.relative_to('C:\\Program Files')
WindowsPath('Internet Explorer/iexplore.exe')

>>> p.exists()
True
Pascale answered 19/1, 2016 at 10:37 Comment(4)
nice demonstration of the APIDetestation
This has also been backported to older versions of Python: pathlib2Lozier
You example is wrong if your path contains for example \a. you should add r in front of the path' string.Unplumbed
I'm doing something very similar to the OP. This is the right answer. The accepted answer uses os.path.split which missed some path elements in my testing. pathlib Path works perfect for meFrohman
S
29

All you need is parent part if you use pathlib.

from pathlib import Path
p = Path(r'C:\Program Files\Internet Explorer\iexplore.exe')
print(p.parent) 

Will output:

C:\Program Files\Internet Explorer    

Case you need all parts (already covered in other answers) use parts:

p = Path(r'C:\Program Files\Internet Explorer\iexplore.exe')
print(p.parts) 

Then you will get a list:

('C:\\', 'Program Files', 'Internet Explorer', 'iexplore.exe')

Saves tone of time.

Southpaw answered 31/5, 2019 at 10:45 Comment(0)
D
5

First, see if you have splitunc() as an available function within os.path. The first item returned should be what you want... but I am on Linux and I do not have this function when I import os and try to use it.

Otherwise, one semi-ugly way that gets the job done 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 retrieving the directory just above the file, and the directory just above that.

Disturbing answered 13/4, 2012 at 23:0 Comment(3)
I edited my entry to show use of rsplit which does what you suggest - but still gives me the path not just the directory name.Stroke
I ended up splitting the path and taking the piece I wanted, it didn't work before but after reading all these answers, I found out what I did wrong.Stroke
I like the way this semi-ugly way works. I change the "\\" by a simple os.sep and it works perfectly to retrieve just a fraction of a path.Sonny
E
3
import os

directory = os.path.abspath('\\') # root directory
print(directory) # e.g. 'C:\'

directory = os.path.abspath('.') # current directory
print(directory) # e.g. 'C:\Users\User\Desktop'

parent_directory, directory_name = os.path.split(directory)
print(directory_name) # e.g. 'Desktop'
parent_parent_directory, parent_directory_name = os.path.split(parent_directory)
print(parent_directory_name) # e.g. 'User'

This should also do the trick.

Echinate answered 7/2, 2020 at 15:37 Comment(0)
C
1

You have to put the entire path as a parameter to os.path.split. See The docs. It doesn't work like string split.

Clinician answered 13/4, 2012 at 22:54 Comment(1)
This won't work on UNC type pathnames on Windows, as the Python docs for os.path stuff state.Disturbing
S
1

This is what I did to extract the piece of the directory:

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

Thank you for your help.

Stroke answered 16/4, 2012 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.