473,387 Members | 1,766 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.

How to check for EOF in a text file

1
Hi...This is my first post....I am a newbie to python programming and I have started a program that uses file handling....My problem is that despite the fact that other programming languages (pascal lets say) has an EOF command python doesnt...

My code is something like that:
Expand|Select|Wrap|Line Numbers
  1. for i in range (99):                     # this is just an example
  2.                 fin.open("text" , "r+")
  3.                 fin.seek(i)
  4.                 text=fin.read(1)
  5.                 fin.close()
  6.                 print fin
  7. i=i+1

The problem is that the file is a document and I need to read every letter individually and stop reading at the end of the document.....Any ideas?
((Sry if this sound a little bit newbish but i am completely new to that stuff))
Oct 14 '07 #1
3 25518
bartonc
6,596 Expert 4TB
Hi...This is my first post....I am a newbie to python programming and I have started a program that uses file handling....My problem is that despite the fact that other programming languages (pascal lets say) has an EOF command python doesnt...

My code is something like that:
Expand|Select|Wrap|Line Numbers
  1. for i in range (99):                     # this is just an example
  2.                 fin.open("text" , "r+")
  3.                 fin.seek(i)
  4.                 text=fin.read(1)
  5.                 fin.close()
  6.                 print fin
  7. i=i+1

The problem is that the file is a document and I need to read every letter individually and stop reading at the end of the document.....Any ideas?
((Sry if this sound a little bit newbish but i am completely new to that stuff))
We generally don't use read() very often in Python. Files are much easier to handle by iterating over them as if they are lists. The docs that that read() retruns and empty string when EOF is encountered:
read( [size])

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.
So:
Expand|Select|Wrap|Line Numbers
  1. f = open(r'D:\My Documents\Python\TheScripts examples\temp.txt')
  2. while True:
  3.     c = f.read(1)
  4.     if not c:
  5.         break
  6.     print c,
  7.  
  8. f.close()
  9.  
  10. t e s t i n g
Oct 14 '07 #2
Smygis
126 100+
I wuld do it.

Expand|Select|Wrap|Line Numbers
  1. for c in open("testfile").read():
  2.     print c,
  3.  
The problem is that the file is a document and I need to read every letter individually and stop reading at the end of the document.....Any ideas?
((Sry if this sound a little bit newbish but i am completely new to that stuff))
Its way better to read in the whole file at once and then process it.
Oct 14 '07 #3
bartonc
6,596 Expert 4TB
I wuld do it.

Expand|Select|Wrap|Line Numbers
  1. for c in open("testfile").read():
  2.     print c,
  3.  


Its way better to read in the whole file at once and then process it.
That's true of small files. On large files you may only get a buffer's worth on a single read().
Oct 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: richardkreidl | last post by:
What I'd like to do is to check the values in an element if they're greater than a certain time and if they are, then change the font color of the time. I have about 10 elements that I will check...
3
by: Matt | last post by:
The user can select a file in the file open dialog using <input type="FILE">, but the user can also enter the full file path in the browse text box manually. My question is if there are JavaScript...
3
by: Matt | last post by:
<input type="file" size=50"> will produce the browse button and browse text box. The user can either select the file from browse button, or enter a path in browse text box manually. My question...
2
by: MikeY | last post by:
Hi all, I am coding window forms in C#. My problem is this: I have created a "Check ListView" or a 'ListView' with checkbox's. I have populated the it with my files from my folders, mps, txt,...
5
by: Paolo | last post by:
Hi all! I have to create an application that receive some packet from an interface and builds a file from those. In the header there are three fields: one is the total lenght of the file, one is...
3
by: H8ComputersSomeDays | last post by:
I want to have my program automatically check for a new version when it is run. My app is in VB.NET (VS 2003). Updates are posted on my website and I would like the app to go to the site and check...
4
by: giftson.john | last post by:
Hi, I am creating an application which migrates all documents from one repository to another repository. Before migration i have to verify all the documents are unique. No duplicates has to be...
10
by: Cliff72 | last post by:
Is there a way in VBA to check if a linked table in the database has been updated? Example: I have a table "LedgerTemp" which is a direct link to a text file on the LAN "Ledger.txt" This text...
1
by: avbergen | last post by:
Hi, I use the following code to read a xml file: While (reader.Read()) If reader.Name = "DATETIME" Then DateVar = reader.ReadString
1
by: jerger | last post by:
I have not made a program or page from start yet. I have made modifications to our signoff asp pages like changing the questions, texts, shortening field lengths etc... I also have copied the files...
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: 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...
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
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
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,...

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.