472,989 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

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 1522
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

- Bjrn Kempn

Sep 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mathias Koerber | last post by:
Hi, I am quite new to PHP (but have experience with perl etc) and am facing a certain issue with PHP 4.2.1's ftp function. I am trying to extend a program to fetch a file from a remote server....
5
by: Giles Brown | last post by:
I'm feeling quite dumb this morning. I'm trying to build a COM server DLL using py2exe and it ain't working. Here's what ain't working... setup_dll.py based on py2exe sample: """from...
6
by: Pierre Rouleau | last post by:
Hi all! I am using Python 2.3.1 on Win32 (NT, 2000). Whenever a file imports the standard tempfile module, Python 2.3.1 issues the following warning: C:\Python23\lib\fcntl.py:7:...
1
by: Matt Garman | last post by:
I've been working on a curses-based application in Python. My application effectively has a series of screens (or windows). When one screen closes, the previous screen should be exactly redrawin...
0
by: Leo Breebaart | last post by:
On MS Windows, I am trying to find out a good default location to save some temporary files. The tempfile module seemed to have exactly what I wanted: >>> import tempfile >>>...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
6
by: James T. Dennis | last post by:
Tonight I discovered something odd in the __doc__ for tempfile as shipped with Python 2.4.4 and 2.5: it says: This module also provides some data items to the user: TMP_MAX - maximum number...
4
by: billiejoex | last post by:
Hi all, I would like to use tempfile module to generate files having unique names excepting that I don't want them to be removed after closing. Does it is possible?
1
by: rparimi | last post by:
Hello pythoners, When I create temporary file using the tempfile module, and forkI) later on in my program, I always see errors when the program exits. Is this because the child process deletes...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.