[Tutor] Question about reading from a file.

2012-08-07 Thread Brad Dutton
I recently discovered how to read from a file but I've having some trouble
with it. I made a short example program that goes like this:


   1. fob = open('c:/python27/a.txt', 'r')
   2.
   3. print fob.read()
   4. print fob.read()

When it runs it returns this:


   1. Hey now brown cow
   2.

It's like it forgets how to read after the first time. This has left me
very confused. Could you offer any reason why this might be happening?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about reading from a file.

2012-08-07 Thread Peter Otten
Brad Dutton wrote:

 I recently discovered how to read from a file but I've having some trouble
 with it. I made a short example program that goes like this:
 
 
1. fob = open('c:/python27/a.txt', 'r')
2.
3. print fob.read()
4. print fob.read()
 
 When it runs it returns this:
 
 
1. Hey now brown cow
2.
 
 It's like it forgets how to read after the first time. This has left me
 very confused. Could you offer any reason why this might be happening?

The open file, 'fob' maintains a pointer into the file that is moved by read 
operations:

 fob = open(a.txt)
 fob.read(3)
'Hey'
 fob.read(3)
' no'
 fob.read(3)
'w b'

If you call the read method without argument the complete (remaining) file 
is read and following read calls will not give any data to read:

 fob.read()
'rown cow\n'
 fob.read()
''

Most of the time the best approach is to open a new file

 for i in range(3):
... with open(a.txt) as f: print f.read().strip()
... 
Hey now brown cow
Hey now brown cow
Hey now brown cow

but it is also possible to move the pointer back to the beginning to the 
file (or elsewhere):

 fob.seek(4)
 fob.read()
'now brown cow\n'
 fob.seek(0)
 fob.read()
'Hey now brown cow\n'


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about reading from a file.

2012-08-07 Thread Mark Lawrence

On 04/08/2012 22:51, Brad Dutton wrote:

I recently discovered how to read from a file but I've having some trouble
with it. I made a short example program that goes like this:


1. fob = open('c:/python27/a.txt', 'r')
2.
3. print fob.read()
4. print fob.read()

When it runs it returns this:


1. Hey now brown cow
2.

It's like it forgets how to read after the first time. This has left me
very confused. Could you offer any reason why this might be happening?



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



Further to Peter Otten's answer you make want to take a look at readline 
or readlines, or check into file iteration.  Depends on what you're 
trying to achieve :)


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about reading from a file.

2012-08-07 Thread Joel Goldstick
On Tue, Aug 7, 2012 at 5:12 AM, Mark Lawrence breamore...@yahoo.co.uk wrote:
 On 04/08/2012 22:51, Brad Dutton wrote:

 I recently discovered how to read from a file but I've having some trouble
 with it. I made a short example program that goes like this:


 1. fob = open('c:/python27/a.txt', 'r')
 2.
 3. print fob.read()
 4. print fob.read()


 When it runs it returns this:


 1. Hey now brown cow
 2.

 It's like it forgets how to read after the first time. This has left me
 very confused. Could you offer any reason why this might be happening?

You need to understand some basic concepts about what a file is in
computer programming.

The other posters have helped you out.  You can find out more here:

http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects

or spend an hour or so (at least) with the results from googling
'python file operation example'

good luck





 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


 Further to Peter Otten's answer you make want to take a look at readline or
 readlines, or check into file iteration.  Depends on what you're trying to
 achieve :)

 --
 Cheers.

 Mark Lawrence.


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about reading from a file.

2012-08-07 Thread Ramchandra Apte

 Imagine a book.

.read(num) reads a page of the book
Now if you call again it goes to the next page.

 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about reading from a file.

2012-08-07 Thread Mark Lawrence

On 07/08/2012 13:37, Ramchandra Apte wrote:


Imagine a book.


.read(num) reads a page of the book
Now if you call again it goes to the next page.



Please don't top post.  This is now the fourth time you've been asked.

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor