How can I check if the listdir result a directory?

P

Plissken.s

I have the following code which list out all the files/directories
under the current directory.

But how can I find out if 'f' is a directory?

for f in os.listdir("."):
// how can I check if 'f' a directory?
 
P

Paul Rubin

for f in os.listdir("."):
// how can I check if 'f' a directory?

Your two approaches are 1) use os.stat (or stat.stat) and check the
mode bits; or 2) use os.listdir and catch the OSError exception you
get if you use it on a non-directory.
 
T

Tim Daneliuk

I have the following code which list out all the files/directories
under the current directory.

But how can I find out if 'f' is a directory?

for f in os.listdir("."):
// how can I check if 'f' a directory?

Try os.path.isdir()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top