How to identify whether a file is a normal file or a directory using python? thanks
+12
A:
os.path.isdir()
and os.path.isfile
should give you what you want. See:
http://docs.python.org/library/os.path.html
PTBNL
2009-06-05 13:50:09
Why the down vote?
PTBNL
2009-06-06 03:08:10
lol - and why not accepted?
justinhj
2010-01-15 22:37:31
+2
A:
import os
if os.path.isdir(d):
print "dir"
else:
print "file"
Dominic Rodger
2009-06-05 13:51:01
A:
erenon
2009-06-05 13:51:32
A:
try this:
import os.path
if os.path.isdir("path/to/your/file"):
print "it's a directory"
else:
print "it's a file"
paffnucy
2009-06-05 13:51:39
+5
A:
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.
retracile
2009-06-05 14:15:07