basic output question

J

John Deas

Hi, I am very new to Python (1 evening...)
I need to process a series of files (toto-1.txt toto-2.txt toto-3.txt
toto-4.txt), and as such I created a small program to go through the
files in a directory. I want to call the script with arguments, like

python script.py toto- 1 1 4

my script is as follow :

import sys
sys.argv
header= sys.argv[1]
start =eval(sys.argv[2])
step =eval(sys.argv[3])
nbit =eval(sys.argv[4])

for i in range(nbit):
filename=header+str(start+i*step)+'.txt'
f=open(filename,'r')
f.read()
f.close()

My problem is that f.read() outputs nothing, and should I print
filename, I can check that they are well formed, and the files sits in
the same directory as my script.

Anyone could help me on this ?
 
A

Alan Isaac

John said:
My problem is that f.read() outputs nothing

Since ``open`` did not give you an IOError,
you did get a handle to the files,
so this suggests that the files you read
are empty...

Alan Isaac
 
W

Wildemar Wildenburger

John said:
Hi, I am very new to Python (1 evening...)
I need to process a series of files (toto-1.txt toto-2.txt toto-3.txt
toto-4.txt), and as such I created a small program to go through the
files in a directory. I want to call the script with arguments, like

python script.py toto- 1 1 4

my script is as follow :

import sys
sys.argv
header= sys.argv[1]
start =eval(sys.argv[2])
step =eval(sys.argv[3])
nbit =eval(sys.argv[4])
Style note: You should probably use int() instead of eval. Not only is
this safer (as in security), it is also safer (as in robust), because it
will throw an error early if you feed it something other than an int.

for i in range(nbit):
filename=header+str(start+i*step)+'.txt'
f=open(filename,'r')
f.read()
f.close()

My problem is that f.read() outputs nothing, and should I print
filename, I can check that they are well formed, and the files sits in
the same directory as my script.
f.read() spills the text into nothingness. You may want to write its
output to a variable. ;)

/W
 

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
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top