Understanding tempfile.TemporaryFile

B

byte8bits

Wondering if someone would help me to better understand tempfile. I
attempt to create a tempfile, write to it, read it, but it is not
behaving as I expect. Any tips?
0
 
J

John Machin

Wondering if someone would help me to better understand tempfile. I
attempt to create a tempfile, write to it, read it, but it is not
behaving as I expect. Any tips?


0

This is nothing particular to your subject; it applies to all files.

x.read() starts reading at the CURRENT POSITION, not at the start of
the file. In all cases above, the then current position was the END of
the file, so x.read() returned a zero-length string.

Check out the seek method.
 
S

Shane Geiger

import tempfile
tmp = tempfile.mktemp()

import os
os.remove(tmp)



Wondering if someone would help me to better understand tempfile. I
attempt to create a tempfile, write to it, read it, but it is not
behaving as I expect. Any tips?


0


--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
S

Steven D'Aprano

Wondering if someone would help me to better understand tempfile. I
attempt to create a tempfile, write to it, read it, but it is not
behaving as I expect. Any tips?

You need to seek to the part of the file you want to read:
'Nobody EXPECTS the Spanish Inquisition!'
 
S

Steven D'Aprano

import tempfile
tmp = tempfile.mktemp()

import os
os.remove(tmp)

Not only does that not answer the Original Poster's question, but I don't
think it does what you seem to think it does.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'




You might like to read help(tempfile.mktemp).

(By the way... the whole point of using tempfile is to avoid needing to
delete the file by hand afterwards.)
 
S

Shane Geiger

Yes, I knew this. Good call, it was just a bad copy and paste example
of lines that showed up close together in a file. My apologies.
import tempfile
tmp = tempfile.mktemp()

import os
os.remove(tmp)

Not only does that not answer the Original Poster's question, but I don't
think it does what you seem to think it does.


'/tmp/tmpZkS0Gj'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'




You might like to read help(tempfile.mktemp).

(By the way... the whole point of using tempfile is to avoid needing to
delete the file by hand afterwards.)


--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
K

Karthik Gurusamy

import tempfile
tmp = tempfile.mktemp()
import os
os.remove(tmp)

Not only does that not answer the Original Poster's question, but I don't
think it does what you seem to think it does.

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'

You might like to read help(tempfile.mktemp).

(By the way... the whole point of using tempfile is to avoid needing to
delete the file by hand afterwards.)

FWIW tempfile.mkstemp needs explicit user deletion. And
tempfile.mkstemp is recommended over tempfile.mktemp due to security
reasons.

Help on function mkstemp in module tempfile:

mkstemp(suffix='', prefix='tmp', dir=None, text=False)
mkstemp([suffix, [prefix, [dir, [text]]]])
User-callable function to create and return a unique temporary
file. The return value is a pair (fd, name) where fd is the
file descriptor returned by os.open, and name is the filename.

If 'suffix' is specified, the file name will end with that suffix,
otherwise there will be no suffix.

If 'prefix' is specified, the file name will begin with that
prefix,
otherwise a default prefix is used.

If 'dir' is specified, the file will be created in that directory,
otherwise a default directory is used.

If 'text' is specified and true, the file is opened in text
mode. Else (the default) the file is opened in binary mode. On
some operating systems, this makes no difference.

The file is readable and writable only by the creating user ID.
If the operating system uses permission bits to indicate whether a
file is executable, the file is executable by no one. The file
descriptor is not inherited by children of this process.

Caller is responsible for deleting the file when done with it.
<-------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top