473,238 Members | 1,441 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,238 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 25489
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.