os.path.isfile() error

7

7stud

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'
 
P

Peter Otten

7stud said:
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'
 
7

7stud

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'

I got it: 'mydir' v. 'dir'
 
J

John Machin

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'

Re your subject: os.path.isfile is nothing to do with the problem.
As is evident from the traceback, the error (which is in *your* code)
was detected in os.path.join, before os.path.isfile was called.

Looking at the source file (posixpath.py), one sees that the offending
"path" (the builtin function/method with no endswith attribute) is the
first formal arg of os.path.join. You have supplied "dir" as the first
actual arg. dir is a builtin function. You meant "mydir" instead.

BTW, don't use "file" (or the name of any other builtin function) for
your own variables. Use meaningful names -- what you have called
"file" is not a file, it is the name of a file. Get pychecker and/or
pylint and run them over your source periodically. They'll detect not
only shadowing builtins but many other types of actual and potential
gotchas.

HTH,
John
 
M

mik3l3374

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'?
 
S

Steve Holden

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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top