os.path.isfile() error

Steve Holden steve at holdenweb.com
Sat Apr 7 07:05:06 EDT 2007


mik3l3374 at gmail.com wrote:
> On Apr 7, 4:56 pm, "7stud" <bbxx789_0... at yahoo.com> 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) )
>> ]
>>
>> 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'
> 
> is 'dir' defined? or is it 'mydir'?
> 
[this is for 7stud, not for the poster] Clearly it should be 'mydir', 
but 'dir' is also defined - it's a built-in function, which is why it 
has no 'endswith' method.

regards
  Steve

-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list