If I have the following file:
file = '/Users/david542/Desktop/work.txt'
I can use os.path.basename(file) to get the file name.
What command would I use to get the directory of the file (i.e., to get "/Users/david542/Desktop") ?
os.path.dirname(file) returns the directory of the passed file name. Alternatively, you can use os.path.split(file) which will give you a tuple containing the directory name and the file name in one call.
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