How can I extract the folder path from file path in Python?
Solution 1
You were almost there with your use of the split
function. You just needed to join the strings, like follows.
>>> import os
>>> '\\'.join(existGDBPath.split('\\')[0:-1])
'T:\\Data\\DBDesign'
Although, I would recommend using the os.path.dirname
function to do this, you just need to pass the string, and it'll do the work for you. Since, you seem to be on windows, consider using the abspath
function too. An example:
>>> import os
>>> os.path.dirname(os.path.abspath(existGDBPath))
'T:\\Data\\DBDesign'
If you want both the file name and the directory path after being split, you can use the os.path.split
function which returns a tuple, as follows.
>>> import os
>>> os.path.split(os.path.abspath(existGDBPath))
('T:\\Data\\DBDesign', 'DBDesign_93_v141b.mdb')
Solution 2
WITH PATHLIB MODULE (UPDATED ANSWER)
One should consider using pathlib for new development. It is in the stdlib for Python3.4, but available on PyPI for earlier versions. This library provides a more object-orented method to manipulate paths <opinion>
and is much easier read and program with </opinion>
.
>>> import pathlib
>>> existGDBPath = pathlib.Path(r'T:\Data\DBDesign\DBDesign_93_v141b.mdb')
>>> wkspFldr = existGDBPath.parent
>>> print wkspFldr
Path('T:\Data\DBDesign')
WITH OS MODULE
Use the os.path module:
>>> import os
>>> existGDBPath = r'T:\Data\DBDesign\DBDesign_93_v141b.mdb'
>>> wkspFldr = os.path.dirname(existGDBPath)
>>> print wkspFldr
'T:\Data\DBDesign'
You can go ahead and assume that if you need to do some sort of filename manipulation it's already been implemented in os.path
. If not, you'll still probably need to use this module as the building block.
Solution 3
The built-in submodule os.path has a function for that very task.
import os
os.path.dirname('T:\Data\DBDesign\DBDesign_93_v141b.mdb')
Solution 4
Here is the code:
import os
existGDBPath = r'T:\Data\DBDesign\DBDesign_93_v141b.mdb'
wkspFldr = os.path.dirname(existGDBPath)
print wkspFldr # T:\Data\DBDesign
Solution 5
Here is my little utility helper for splitting paths int file, path tokens:
import os
# usage: file, path = splitPath(s)
def splitPath(s):
f = os.path.basename(s)
p = s[:-(len(f))-1]
return f, p
Related videos on Youtube
Genspec
Updated on September 03, 2021Comments
-
Genspec over 2 years
I would like to get just the folder path from the full path to a file.
For example
T:\Data\DBDesign\DBDesign_93_v141b.mdb
and I would like to get justT:\Data\DBDesign
(excluding the\DBDesign_93_v141b.mdb
).I have tried something like this:
existGDBPath = r'T:\Data\DBDesign\DBDesign_93_v141b.mdb' wkspFldr = str(existGDBPath.split('\\')[0:-1]) print wkspFldr
but it gave me a result like this:
['T:', 'Data', 'DBDesign']
which is not the result that I require (being
T:\Data\DBDesign
).Any ideas on how I can get the path to my file?
-
Dave Babbitt over 4 years
os.sep.join(existGDBPath.split(os.sep)[:-1]
looks prettier. -
iedmrc about 4 years
Path().parent
what I was looking for! -
Sébastien Dawans over 3 years
'\\'.join()
is not portable, useos.path.join()
instead -
Petr Vepřek over 3 yearsUsing str methods
split
andjoin
is not portable (think/
on *nix).os.path
has all methods you need. -
Vexen Crabtree about 3 yearsMarked-up simply because you stuck <opinion /> in tags!