mkstemp on pre-2.3 Python

D

djw

Hi, c.l.p'ers-

I working on some code that creates a temporary file on Linux. I found some
code that the BDFL posted previously on the newsgroup and adapted it for my
use. I can't get the "pre-2.3" code to work.


try:
make_temp_file = tempfile.mkstemp # 2.3+
except AttributeError:
def make_temp_file( suffix='', prefix='', dir='', text=False ): #pre-2.3
path = tempfile.mktemp( suffix )
fd = os.open( path, os.O_WRONLY|os.O_CREAT|os.O_EXCL, 0700 )
os.unlink( path )
return ( os.fdopen( fd, 'w+b' ), path )



Here's the error:

File "utils.py", line 583, in make_temp_file
return ( os.fdopen( fd, 'w+b' ), path )
OSError: [Errno 22] Invalid argument

Anybody have any ideas on what I am doing wrong?

Thanks,

Don
 
N

Nick Craig-Wood

djw said:
Hi, c.l.p'ers-

I working on some code that creates a temporary file on Linux. I found some
code that the BDFL posted previously on the newsgroup and adapted it for my
use. I can't get the "pre-2.3" code to work.


try:
make_temp_file = tempfile.mkstemp # 2.3+
except AttributeError:
def make_temp_file( suffix='', prefix='', dir='', text=False ): #pre-2.3
path = tempfile.mktemp( suffix )
fd = os.open( path, os.O_WRONLY|os.O_CREAT|os.O_EXCL, 0700 )
os.unlink( path )
return ( os.fdopen( fd, 'w+b' ), path )



Here's the error:

File "utils.py", line 583, in make_temp_file
return ( os.fdopen( fd, 'w+b' ), path )
OSError: [Errno 22] Invalid argument

Anybody have any ideas on what I am doing wrong?

Replace the os.O_WRONLY with os.O_RDWR and all will be fine
 
D

djw

Thank you very much, that was the ticket!

-Don

djw said:
Hi, c.l.p'ers-

I working on some code that creates a temporary file on Linux. I found
some code that the BDFL posted previously on the newsgroup and adapted
it for my use. I can't get the "pre-2.3" code to work.


try:
make_temp_file = tempfile.mkstemp # 2.3+
except AttributeError:
def make_temp_file( suffix='', prefix='', dir='', text=False ):
#pre-2.3
path = tempfile.mktemp( suffix )
fd = os.open( path, os.O_WRONLY|os.O_CREAT|os.O_EXCL, 0700 )
os.unlink( path )
return ( os.fdopen( fd, 'w+b' ), path )



Here's the error:

File "utils.py", line 583, in make_temp_file
return ( os.fdopen( fd, 'w+b' ), path )
OSError: [Errno 22] Invalid argument

Anybody have any ideas on what I am doing wrong?

Replace the os.O_WRONLY with os.O_RDWR and all will be fine
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,153
Latest member
NamKaufman
Top