Easy question on opening a file

N

news.west.cox.net

I want to check a directory, see if there are any files in it... then open
each one and do something.

I have this...

files = os.listdir('/direcory')
if len(files) > 0:
for file in files:
f1 = file(file, "r")
do some other stuff

But this raises a TypeError:
TypeError: 'str' object is not callable

So I tried replacing the line f1 = file(file, "r") with f1 = file('%s'
%file, "r")
But that does not work either.

I know this is easy, anyone care to point out the solution.

Thanks.

--


Sean Berry ~ Internet Systems Programmer
BuildingOnline Inc.
The Building Industry's Web Design and Marketing Agency
Celebrating our 9th year in business, founded Aug. 1995
Ph: 888-496-6648 ~ Fax: 949-496-0036
--> Web Design Agency site: http://www.BuildingOnline.net
--> Building Industry Portal: http://www.BuildingOnline.com
--> Building Industry News: http://www.BuildingOnline.com/news/
--> Home Plans: http://www.eHomePlans.com
 
N

news.west.cox.net

news.west.cox.net said:
I want to check a directory, see if there are any files in it... then open
each one and do something.

I have this...

files = os.listdir('/direcory')
if len(files) > 0:
for file in files:
f1 = file(file, "r")
do some other stuff

Figured out my mistake already.... 2 minutes later.
I should not be using the word file, which is a python keyword.
 
J

Jeremy Bowers

files = os.listdir('/direcory')
if len(files) > 0:
for file in files:
f1 = file(file, "r")
do some other stuff

This will help you:

files = os.listdir('/directory')
print "The name 'file' is bound to:", repr(file)
for file in files:
print "Now the name 'file' is bound to:", repr(file)

(Probably one of the last little warts I get bitten by often is that the
builtins are named a little *too* conveniently... :) OK, even "wart" is
strong, but you know what I mean.)
 
A

Alex Martelli

news.west.cox.net said:
Figured out my mistake already.... 2 minutes later.
I should not be using the word file, which is a python keyword.

Not a keyword, or else you would have seen a syntax error as soon as you
tried using it as a normal identifier. Just a built-in name, which
you're perfectly entitle to bind it as an identifier -- but if you do,
of course, then you can't use that same name for two different meanings
in the same scope. In the call 'file(file, ...", the two occurrences of
identifier 'file' MUST refer to the same object.

It's probably best to avoid using Python builtin's names for other
purposes, of course, as you suggest. But there are many such names and
you need not memorize them for the purpose -- if your modules and
functions never use some obscure built-in such as, say, 'apply', there's
no harm done if you use that name as an identifier.

A side note: I think you're opening subdirectories as well as files (or
rather, you''re trying to -- the attempt will probably raise an
exception). You may use a try/except clause to deal with that.


Alex
 

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

Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top