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.
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.
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.
You can import os
>>> filepath '/a/path/to/my/file.txt' >>> os.path.dirname(filepath) '/a/path/to/my' >>>
Use os.path.dirname(filename)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With