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

Testing for EOF ?

I am reasonably new to python and am trying to read several lines of text
from an open file.

Typically in other languages I used to use code like:

while not EOF()
read_text_in
...
How is this achieved with python ?

Regards
Pete

Jul 18 '05 #1
2 1613
On Apr 4, 2005 12:21 PM, Pete Moscatt <pg*******@optushome.com.au> wrote:
I am reasonably new to python and am trying to read several lines of text
from an open file.


my_file = open('whatever.txt', 'r')
for line in my_file:
print line # Or whatever

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 18 '05 #2
On Apr 4, 2005 12:36 PM, Peter Moscatt <pg*******@optushome.com.au> wrote:
Thanks Simon,

So the code should look like:

f=open(myfile,"r")

for some_var in f:
text=f.readline()
print text

Do I have this correct ?


Nearly - you don't need the text=f.readline() bit. A file object (as
returned to you by open()) is an iterator, and iterating over it gives
you the lines directly.

Why not give it a go? The nice thing about Python is how easy it is to
try stuff like this out at the interactive prompt. Err, *one* of the
nice things, that is. ;-)

BTW, newbies are more than welcome here, but there is a python-tutor
list[1] which might be more your speed if you are quite new to Python.
There are also a number of tutorials available[2] . I'd recommend
either the standard tutorial[3] or Dive Into Python[4] for someone
like you who already knows how to program but doesn't know Python.

Welcome to Python, and have fun.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
[1] http://mail.python.org/mailman/listinfo/tutor
[2] http://www.python.org/moin/BeginnersGuide/Programmers
[3] http://www.python.org/doc/current/tut/
[4] http://diveintopython.org/
Jul 18 '05 #3

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

Similar topics

4
by: Hugh Cowan | last post by:
Hello, I don't program full-time (anymore), but I do try and stay on-top of the latest technologies and like most are always trying to upgrade my skills and remain current (as much as is...
0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
0
by: Brian Russell | last post by:
We have three servers (beyond my development box) in our organization. The first is a testing server that has IIS and SQL Server on it. The second is another testing server that also has IIS and...
4
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
58
by: nw | last post by:
Hi, I have been asked to teach a short course on testing in C++. Until now I have used my own testing classes (which from what I've seen seem similar to the boost unit testing classes)....
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
0
by: Matthew Fitzgibbons | last post by:
I'm by no means a testing expert, but I'll take a crack at it. Casey McGinty wrote: I've never run into this. Rule of thumb: always separate software from hardware. Write mock classes or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.