Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path to a file without basename [duplicate]

Tags:

python

How can I get the path of a file without the file basename?

Something like /a/path/to/my/file.txt --> /a/path/to/my/

Tried with .split() without success.

like image 572
nlassaux Avatar asked Aug 01 '12 10:08

nlassaux


People also ask

What is a basename path?

basename() is a built-in Python method used to get the base name in the specified path. The path. basename() function takes an argument of a specified path and returns the base name of the pathname path.

How do I strip a file extension in Python?

To remove the extension from a filename using Python, the easiest way is with the os module path. basename() and path. splitext() functions. You can also use the pathlib module and Path and then access the attribute 'stem' to remove the extension from a filename.


2 Answers

You can import os

>>> filepath '/a/path/to/my/file.txt' >>> os.path.dirname(filepath) '/a/path/to/my' >>>  
like image 35
aayoubi Avatar answered Sep 18 '22 00:09

aayoubi


Use os.path.dirname(filename).

like image 73
Daniel Roseman Avatar answered Sep 17 '22 00:09

Daniel Roseman