How do you check whether a file is a normal file or a directory using python?
How do you check whether a file is a normal file or a directory using python?
os.path.isdir() and os.path.isfile() should give you what you want. See:
http://docs.python.org/library/os.path.html
As other answers have said, os.path.isdir() and os.path.isfile() are what you want. However, you need to keep in mind that these are not the only two cases. Use os.path.islink() for symlinks for instance. Furthermore, these all return False if the file does not exist, so you'll probably want to check with os.path.exists() as well.
try this:
import os.path
if os.path.isdir("path/to/your/file"):
print "it's a directory"
else:
print "it's a file"
To check if a file/directory exists:
os.path.exists(<path>)
To check if a path is a directory:
os.path.isdir(<path>)
To check if a path is a file:
os.path.isfile(<path>)