os.path.isfile() error

Peter Otten __peter__ at web.de
Sat Apr 7 11:08:27 CEST 2007


7stud wrote:

> Here's the code:
> ------------
> import os, os.path, pprint
> 
> mydir = "/Users/me/2testing"
> 
> files = [file for file in os.listdir(mydir)]
> pprint.pprint(files)
> 
> print os.path.join(mydir, "helloWorld.py")
> 
> files = [file
> for file in os.listdir(mydir)
> if os.path.isfile(os.path.join(dir, file) )

That should be mydir, not dir.

> ]
> 
> pprint.pprint(files)
> ----output:----------------
> 
> ['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
> /Users/me/2testing/helloWorld.py
> Traceback (most recent call last):
>   File "test1.py", line 16, in ?
>     files = [file
>   File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
> python2.3/posixpath.py", line 62, in join
>     elif path == '' or path.endswith('/'):
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'endswith'




More information about the Python-list mailing list