[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

RE: os.tmpfile

jyoung79

1/2/2008 2:23:00 PM

> 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

Erik, I am going to be displaying sections of text in the Terminal Window on OS X.
I wanted to format the text in a specific way and thought it might be quicker to
output all the text to a temporary file that I could quickly read sections from instead
of storing in memory. Not sure if this is the most efficient way to do this or not but
thought at least it'd be a good way to learn something new in Python. I was
assuming tmpfile() would automatically create some sort of temporary file that
would automatically delete itself when the code was finished.

--

> Try this:

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

redawgts, thank you very much for the example! I appreciate you showing
me how this works!

--

> Please don't use os.tmpfile(). It's not safe and exists only for legacy
> reasons. The tempfile module contains methods to create safe temporary
> files and directories.

> Christian

Thanks Christian for this info! I'll look into using the tempfile module instead.

Thank you all for sharing your knowledge of Python... this is extremely helpful
to me!

Jay
1 Answer

Erik Max Francis

1/2/2008 7:28:00 PM

0

jyoung79@kc.rr.com wrote:

> Erik, I am going to be displaying sections of text in the Terminal Window on OS X.
> I wanted to format the text in a specific way and thought it might be quicker to
> output all the text to a temporary file that I could quickly read sections from instead
> of storing in memory. Not sure if this is the most efficient way to do this or not but
> thought at least it'd be a good way to learn something new in Python. I was
> assuming tmpfile() would automatically create some sort of temporary file that
> would automatically delete itself when the code was finished.

It is more likely that keeping it in a list will be more efficient, and
easier to handle anyway.

--
Erik Max Francis && max@alcyone.com && http://www.alcyon...
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
When angry, count four; when very angry, swear.
-- Mark Twain