473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

os.path.isfile() error

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'

Apr 7 '07 #1
5 8111
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'
Apr 7 '07 #2
On Apr 7, 2:56 am, "7stud" <bbxx789_0...@yahoo.comwrote:
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'
mydir = "/Users/me/2testing"
...
if os.path.isfile(os.path.join(dir, file) )
Apr 7 '07 #3
On Apr 7, 6:56 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
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

Apr 7 '07 #4
On Apr 7, 4:56 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
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'?

Apr 7 '07 #5
mi*******@gmail.com wrote:
On Apr 7, 4:56 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
>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

Apr 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: kimes | last post by:
I've just started digging into how python works.. I found that other mudules are clearly declared like one file per a module.. But the only os.path doesn't have their own file.. ye I know is...
70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
5
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for...
6
by: NickP | last post by:
Hi there, The following returns "False"... MsgBox(New Uri("ftp://me.pop.com/pop.bmp").IsFile.ToString) Is there any particular reason? It seems like a pretty poor function to me as that is...
10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
10
by: ppaterson | last post by:
Can os.path.isfile(x) ever return True after os.remove(x) has successfully completed? (Windows 2003, Python 2.3) We had a couple of failures in a server application that we cannot yet reproduce...
2
by: 7stud | last post by:
Here is a program to print out the files in a directory: ----------- import os myfiles = os.listdir("../") print myfiles for afile in myfiles: print afile
0
by: Gabriel Genellina | last post by:
En Wed, 27 Aug 2008 01:15:58 -0300, aditya shukla <adityashukla1983@gmail.comescribi�: Can't you get the program name and path from the place you got it originally, before adding it to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.