help with using temporary files

G

Gerard Flanagan

Hello

I'm sure its basic but I'm confused about the error I get with the
following code. Any help on basic tempfile usage?


ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
'c:\\docume~1\\gerard\\locals~1\\temp\\tmpxqn4yl'


Thanks

Gerard
 
J

Jeremy Jones

Gerard said:
Hello

I'm sure its basic but I'm confused about the error I get with the
following code. Any help on basic tempfile usage?


ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

c:\docume~1\gerard\locals~1\temp\tmpxqn4yl
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory:
'c:\\docume~1\\gerard\\locals~1\\temp\\tmpxqn4yl'


Thanks

Gerard

It gets created:

In [24]: import tempfile
In [25]: t = tempfile.NamedTemporaryFile()
In [26]: t.name
Out[26]: '/tmp/tmp9bmhap'
In [27]: ls -l /tmp/tmp*
-rw------- 1 jmjones jmjones 0 Nov 22 11:15 /tmp/tmp9bmhap

In [28]: t.write("123")

In [29]: t.flush()

In [30]: ls -l /tmp/tmp*
-rw------- 1 jmjones jmjones 3 Nov 22 11:15 /tmp/tmp9bmhap

In [31]: t.close()

In [32]: ls -l /tmp/tmp*
ls: /tmp/tmp*: No such file or directory



From the docstring, it gets automatically deleted on close:

def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
prefix=template, dir=None):
"""Create and return a temporary file.
Arguments:
'prefix', 'suffix', 'dir' -- as for mkstemp.
'mode' -- the mode argument to os.fdopen (default "w+b").
'bufsize' -- the buffer size argument to os.fdopen (default -1).
The file is created as mkstemp() would do it.

Returns an object with a file-like interface; the name of the file
is accessible as file.name. The file will be automatically deleted
when it is closed.
"""

HTH,

- jmj
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top