471,319 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

tempfile.mkstemp and os.fdopen

Hi there.
I'm trying to generate a brand new file with a unique name by using
tempfile.mkstemp().
In conjunction I used os.fdopen() to get a wrapper around file
properties (write & read methods, and so on...) but 'name' attribute
does not contain the correct file name. Why?
>>import os
import tempfile
fileno, name = tempfile.mkstemp(prefix='ftpd.', dir=os.getcwd())
fd = os.fdopen(fileno, 'wb')
fd.name
<fdopen>

Moreover, I'd like to know if I'm doing fine. Does this approach avoid
race conditions or other types of security problems?

Thanks in advance

Aug 28 '07 #1
9 3602
On Aug 28, 1:55 pm, billiejoex <gne...@gmail.comwrote:
In conjunction I used os.fdopen() to get a wrapper around file
properties (write & read methods, and so on...) but 'name' attribute
does not contain the correct file name. Why?
>import os
import tempfile
fileno, name = tempfile.mkstemp(prefix='ftpd.', dir=os.getcwd())
fd = os.fdopen(fileno, 'wb')
fd.name

<fdopen>

Moreover, I'd like to know if I'm doing fine. Does this approach avoid
race conditions or other types of security problems?

Thanks in advance
There seems to be a feature request for this:

http://bugs.python.org/issue1625576


Aug 28 '07 #2
In article <11**********************@19g2000hsx.googlegroups. com>,
billiejoex <gn****@gmail.comwrote:
Hi there.
I'm trying to generate a brand new file with a unique name by using
tempfile.mkstemp().
In conjunction I used os.fdopen() to get a wrapper around file
properties (write & read methods, and so on...) but 'name' attribute
does not contain the correct file name. Why?
>import os
import tempfile
fileno, name = tempfile.mkstemp(prefix='ftpd.', dir=os.getcwd())
fd = os.fdopen(fileno, 'wb')
fd.name
<fdopen>

Moreover, I'd like to know if I'm doing fine. Does this approach avoid
race conditions or other types of security problems?

Thanks in advance
In brief, since os.fdopen() only has access to the file descriptor, it
does not have a convenient way to obtain the file's name. However, you
might also want to look at the TemporaryFile and NamedTemporaryFile
classes in the tempfile module -- these expose a file-like API,
including a .name attribute.

Assuming tempfile.mkstemp() is implemented properly, I think what you
are doing should be sufficient to avoid the obvious file-creation race
condition.

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Aug 28 '07 #3
billiejoex <gn****@gmail.comwrote:
I'm trying to generate a brand new file with a unique name by using
tempfile.mkstemp().

Moreover, I'd like to know if I'm doing fine. Does this approach avoid
race conditions
This is a reasonably secure way of doing things. It can't race under
unix at least (dunno about windows) unless your dir is on NFS.

If you want more security then make sure dir isn't publically
writeable.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 28 '07 #4
Thanks all.
Another question: I have to open file for writing ('wb') but I noticed
that both tempfile.mkstemp() and os.fdopen() accept a "mode" argument.
It's not clear *when* do I have to specify such mode. When using
tempfile.mkstemp?
>>fileno, name = tempfile.mkstemp(text=False)
fd = os.fdopen(fileno)
....or when using os.fdopen()?
>>fileno, name = tempfile.mkstemp()
fd = os.fdopen(fileno, mode='wb')
Moreover, what happens if I specify "text" mode when using mkstemp and
"binary" mode when using fdopen?
>>fileno, name = tempfile.mkstemp(text=True)
fd = os.fdopen(fileno, 'wb')

PS - I think that tempfile.mkstemp docstring should be enhanced to
cover such and other questions (e.g. I find reasonable that every user
using tempfile.mkstemp() should use also os.fdopen() in conjunction
but this isn't mentioned).

Aug 29 '07 #5
On 28 ago, 22:21, billiejoex <gne...@gmail.comwrote:
Another question: I have to open file for writing ('wb') but I noticed
that both tempfile.mkstemp() and os.fdopen() accept a "mode" argument.
It's not clear *when* do I have to specify such mode. When using
tempfile.mkstemp?

Moreover, what happens if I specify "text" mode when using mkstemp and
"binary" mode when using fdopen?

PS - I think that tempfile.mkstemp docstring should be enhanced to
cover such and other questions (e.g. I find reasonable that every user
using tempfile.mkstemp() should use also os.fdopen() in conjunction
but this isn't mentioned).
As someone already suggested, why don't you use TemporaryFile or
NamedTemporaryFile and avoid such problems?

--
Gabriel Genellina

Aug 29 '07 #6
On Aug 28, 7:55 pm, billiejoex <gne...@gmail.comwrote:
Hi there.
I'm trying to generate a brand new file with a unique name by using
tempfile.mkstemp().
In conjunction I used os.fdopen() to get a wrapper around file
properties (write & read methods, and so on...) but 'name' attribute
does not contain the correct file name. Why?
import tempfile, os

class TempFile(object):

def __init__(self, fd, fname):
self._fileobj = os.fdopen(fd, 'w+b')
self.name = fname

def __getattr__(self, attr):
return getattr(self._fileobj, attr)

def mktempfile(dir=None, suffix='.tmp'):
return TempFile(*tempfile.mkstemp(dir=dir, suffix=suffix))

Aug 29 '07 #7
In message <Michael.J.Fromberger-8FC07C.15483428082007@localhost>, Michael
J. Fromberger wrote:
... since os.fdopen() only has access to the file descriptor, it
does not have a convenient way to obtain the file's name.
You can do this under Linux as follows:

os.readlink("/proc/%d/fd/%d" % (os.getpid(), fileno))

Aug 29 '07 #8
Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
In message <Michael.J.Fromberger-8FC07C.15483428082007@localhost>, Michael
J. Fromberger wrote:
... since os.fdopen() only has access to the file descriptor, it
does not have a convenient way to obtain the file's name.

You can do this under Linux as follows:

os.readlink("/proc/%d/fd/%d" % (os.getpid(), fileno))
A good idea! You can write this slightly more succinctly as

os.readlink("/proc/self/fd/%d" % fileno)

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 29 '07 #9
Gabriel Genellina wrote:
As someone already suggested, why don't you use TemporaryFile or
NamedTemporaryFile and avoid such problems?
Because I don't want file to be removed after closing.

Aug 29 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Thomas Guettler | last post: by
2 posts views Thread by marco | last post: by
1 post views Thread by Jonathan Wright | last post: by
5 posts views Thread by Martin McCormick | last post: by
5 posts views Thread by Gregory Piñero | last post: by
reply views Thread by Colin Wildsmith | last post: by
6 posts views Thread by James T. Dennis | last post: by
4 posts views Thread by billiejoex | last post: by
7 posts views Thread by byte8bits | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.