Reputation: 291
Is there a way to reliably determine whether a given path is a folder or a file?
I'm writing a directory downloading tool and I need to determine whether this path is a folder, because folders are traversed rather than downloaded.
Currently I use pathlib
's .suffix
feature, but that is a simple split and breaks on foldernames with periods in them. Another way is to check for trailing slash, which should be fairly reliable on URLs copypasted from the browser, but I'd rather not break on input without a trailing slash.
I guess otherwise I could include a whitelist of acceptable extensions, but holy hacky batman.
EDIT: Example: '/home/ripdog/data/2010.09.19%20%5bFELT-001%5d%20Milky%20Wink%20%5b%e4%be%8b%e5%a4%a7%e7%a5%adSP%5d/'
As you can see, there are 2 periods in there at the start of the final foldername.
Upvotes: 0
Views: 36
Reputation: 32189
You can do that using:
os.path.isdir(name)
This will evaluate True
if a given argument is a directory and False
otherwise.
>>> import os
>>> os.path.isdir(name)
Upvotes: 2