473,394 Members | 1,671 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,394 software developers and data experts.

Easy question on opening a file

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
Jul 18 '05 #1
3 1444

"news.west.cox.net" <se*********@cox.net> wrote in message
news:3_t1d.296421$Oi.201894@fed1read04...
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.


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

Jul 18 '05 #2
On Mon, 13 Sep 2004 20:57:35 -0700, news.west.cox.net wrote:
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.)
Jul 18 '05 #3
news.west.cox.net <se*********@cox.net> wrote:
...
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.


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
Jul 18 '05 #4

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

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
5
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new FileStream(@"C:\Data.txt", FileMode.Open,...
4
by: Markus Stoeger | last post by:
Hello! I'm using the FileSystemWatcher to get notified when new files are created in a directory. Once a file appears in the directory I want to read it's contents into memory and do some...
14
by: google | last post by:
I am creating a new database for use within our company, that I'd like to make reasonably secure (short of a true server based solution). The back-end of a non-server based database seems to be...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
7
by: jaysherby | last post by:
Can someone tell me what's the absolute easiest way of putting an image on to the screen in Python without just opening it up in an application? What's the simplest way to just put something up on...
2
by: Craig | last post by:
Hi there, I'm trying to open colour BMPs using PIL and I'm getting the following errors. Opening a 16 colour BMP I get: Traceback (most recent call last): File "<pyshell#3>", line 1, in...
8
by: Max Steel | last post by:
Hey gang, I'm new to python coding. I'm trying to find the simplest way to open a text file (on the same server) and display it's content. The text file is plain text (no markup language of any...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.