tags:

views:

254

answers:

5

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
Why the down vote?
PTBNL
lol - and why not accepted?
justinhj
+2  A: 
import os

if os.path.isdir(d):
    print "dir"
else:
    print "file"
Dominic Rodger
A: 
erenon
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
+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