python - check if it's File -


i have written small code check if it's file. expecting should "yes" print did not get. doing silly mistake.

os.listdir(os.getcwd()+"/../py") = ['a.py', 'a.pyc'] >>> _a in a: ...  if os.path.isfile(_a): ...     print "yes" 

you need provide full path; providing relative path, python looks in current working directory these , there no file named a.py in os.getcwd().

start storing path other directory in variable:

path = os.path.abspath('../py') name in os.listdir(path):     if os.path.isfile(os.path.join(path, name):         print "yes", name, "is file!" 

i used os.path.abspath() instead of os.getcwd() turn relative path normalized absolute path, , used os.path.join() use path construct absolute paths list of names os.listdir() returned.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -