os.tmpfile()

jyoung79 at kc.rr.com jyoung79 at kc.rr.com
Wed Jan 2 09:23:07 EST 2008


> 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



More information about the Python-list mailing list