python 2.7 - Using os.listdir(), see if an object is a file or a directory -


here code wrote:

import os      item in os.listdir("c:/"):         if os.path.isfile(item):             print(item + " file")         elif os.path.isdir(item):             print(item + " dir")         else:             print("unknown!") 

response:

unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown! unknown!  process finished exit code 0 

what did wrong. think maybe because folder locked or encrypted. please give hand!

thanks lot

you need pass complete path isfile() , isdir().

import os   path = "c:" item in os.listdir(path):     item = os.path.join(path, item)     if os.path.isfile(item):         print(item + " file")     elif os.path.isdir(item):         print(item + " dir")     else:         print("unknown!") 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -