473,412 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,412 software developers and data experts.

basic output question

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 ?
Jan 25 '08 #1
2 915
John Deas wrote:
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
Jan 26 '08 #2
John Deas wrote:
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
Jan 26 '08 #3

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

Similar topics

7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
3
by: Martin | last post by:
How does one set up basic authentication on an HttpListener? I know I need to set the HttpListener.AuthenticationSchemes to AuthenticationSchemes.Basic but then I'm unsure how and against what...
2
by: Key9 | last post by:
Question about basic input output. HI All I am a new C++er Suppose I am designing a i/o device . system have screens . system have keyboards.
4
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see...
1
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given...
7
by: Joey | last post by:
I don't care what .net language this come in but I really need to determine if a disk is dynamic or basic. I have posted something in the WMI group but no one knows how to do it. Does anyone have...
5
by: =?Utf-8?B?VG9ueSBXaXNzbGVy?= | last post by:
Hello everyone- I am hoping a couple of you will have some ideas on this one... I have a webform created in visual basic 2005. Basically, the webform gather answers to the questions asked on...
13
by: John Simeon | last post by:
I understand that to creat a file output stream for example, I have to include <fstreamand then: ofstream fout; which will create an object of type ofstream that I can use to interact with a...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
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
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.