[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

os.tmpfile

jyoung79

1/2/2008 4:03:00 AM

Can anyone elaborate on how 'os.tmpfile()' works? I was thinking it would create some sort of temporary file I could quickly add text too and then when I was finished would automatically get rid of it. Here's my questions:

1. Does it actually create a file somewhere? If so, where does it store it? Does it have to manually be deleted afterwards?
3. How do you use it? I tried the following, but it doesn't seem to work:

>>> import os
>>> c = os.tmpfile()
>>> c.write('dude')
>>> c.read()
''
>>> os.path.exists(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py", line 171, in exists
TypeError: coercing to Unicode: need string or buffer, file found
>>> type(c)
<type 'file'>
>>> os.path.basename(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py", line 112, in basename
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py", line 77, in split
AttributeError: 'file' object has no attribute 'rfind'
>>> for x in c:
.... print x
....
>>>

Can you actually 'write' to this file? And if so, do you have to 'close()' it when you're done with it? Thanks for your help with this... I'm still learning Python and haven't been able to find out much about this in the documentation or on-line.

Jay
2 Answers

Erik Max Francis

1/2/2008 4:40:00 AM

0

jyoung79@kc.rr.com wrote:

> Can anyone elaborate on how 'os.tmpfile()' works? I was thinking it would create some sort of temporary file I could quickly add text too and then when I was finished would automatically get rid of it. Here's my questions:
...
> Can you actually 'write' to this file? And if so, do you have to 'close()' it when you're done with it? Thanks for your help with this... I'm still learning Python and haven't been able to find out much about this in the documentation or on-line.

It's a file. You read strings from it and write strings to it. It
isn't a string itself. Given that what you're trying to do doesn't make
any sense, it's hard to know where to begin to identify what's confusing
you.

--
Erik Max Francis && max@alcyone.com && http://www.alcyon...
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
And though the odds say improbable / What do they know
-- Stevie Wonder

redawgts

1/2/2008 6:41:00 AM

0

Try this:

>>> import os
>>> c = os.tmpfile()
>>> c.write('dude')
>>> c.seek(0)
>>> c.read()
'dude'