468,549 Members | 2,383 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

problems using pythom tempfile module

Hello everyone,

I'm trying to test the tempfile module with the following script,
which basically creates a temporary file, fills the file with some
test data and prints it.

import tempfile

t = tempfile.TemporaryFile()
t.write("lalalala")
t.flush()
print t.read()

Unfortunately, the print statement gives me an empty string. Can
somebody tell me what I'm doing wrong ?
regards Samir

Sep 15 '07 #1
4 1384
On Sep 15, 11:11 pm, "samir....@googlemail.com"
<samir....@googlemail.comwrote:
Hello everyone,

I'm trying to test the tempfile module with the following script,
which basically creates a temporary file, fills the file with some
test data and prints it.

import tempfile

t = tempfile.TemporaryFile()
t.write("lalalala")
t.flush()
print t.read()

Unfortunately, the print statement gives me an empty string. Can
somebody tell me what I'm doing wrong ?

regards Samir
Do a t.seek(0) before you do the read to "rewind" the file and then it
should work.

Sep 15 '07 #2
On Sep 15, 5:24 pm, buffi <bjorn.kem...@gmail.comwrote:
On Sep 15, 11:11 pm, "samir....@googlemail.com"

<samir....@googlemail.comwrote:
Hello everyone,
I'm trying to test the tempfile module with the following script,
which basically creates a temporary file, fills the file with some
test data and prints it.
import tempfile
t = tempfile.TemporaryFile()
t.write("lalalala")
t.flush()
print t.read()
Unfortunately, the print statement gives me an empty string. Can
somebody tell me what I'm doing wrong ?
regards Samir

Do a t.seek(0) before you do the read to "rewind" the file and then it
should work.
Ok, this really worked. Can you elaborate why I have to insert this
statement?

Sep 15 '07 #3
sa*******@googlemail.com wrote:
On Sep 15, 5:24 pm, buffi <bjorn.kem...@gmail.comwrote:
>On Sep 15, 11:11 pm, "samir....@googlemail.com"

<samir....@googlemail.comwrote:
>>Hello everyone,
I'm trying to test the tempfile module with the following script,
which basically creates a temporary file, fills the file with some
test data and prints it.
import tempfile
t = tempfile.TemporaryFile()
t.write("lalalala")
t.flush()
print t.read()
Unfortunately, the print statement gives me an empty string. Can
somebody tell me what I'm doing wrong ?
regards Samir
Do a t.seek(0) before you do the read to "rewind" the file and then it
should work.

Ok, this really worked. Can you elaborate why I have to insert this
statement?
Each file has a "current position". As you write a file the current
position moves to stay just ahead of what's been written. So if you read
it without resetting the current position (back to the beginning with
seek(0)) you will get an immediate end of file (i.e. 0 bytes) returned.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 15 '07 #4
Pretend that you have a number that is always pointing somewhere in
your temporary file.
It starts a 0.
If you then write "lalalala" (8 characters) it will point after these
at position 8, so that you can write more stuff after your previous
text later by calling write.

The read method reads all the data from the current position of the
"pointer" to the end of the file. If you simply call it after writing,
you wont get anything since you are already at the end of the file.
Therefore you "rewind" the pointer back to the start by calling seek
which takes the position in the file to go to...
your_file.seek(0) returns you to the start of the file to read all of
its data

- Björn Kempén

Sep 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mathias Koerber | last post: by
5 posts views Thread by Giles Brown | last post: by
1 post views Thread by Matt Garman | last post: by
reply views Thread by Leo Breebaart | last post: by
1 post views Thread by jmalone | last post: by
6 posts views Thread by James T. Dennis | last post: by
4 posts views Thread by billiejoex | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.