471,330 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 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 25198
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

Post your reply

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

Similar topics

reply views Thread by richardkreidl | last post: by
3 posts views Thread by H8ComputersSomeDays | last post: by
4 posts views Thread by giftson.john | last post: by
reply views Thread by rosydwin | last post: by

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.