mkstemp on pre-2.3 Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • djw

    #1

    mkstemp on pre-2.3 Python

    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.mkstem p # 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_EX CL, 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

  • Nick Craig-Wood

    #2
    Re: mkstemp on pre-2.3 Python

    djw <donald.welch@h p.com> wrote:[color=blue]
    > 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.mkstem p # 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_EX CL, 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?[/color]

    Replace the os.O_WRONLY with os.O_RDWR and all will be fine
    [color=blue][color=green][color=darkred]
    >>> import tempfile, os
    >>> suffix=""
    >>> path = tempfile.mktemp ( suffix )
    >>> fd = os.open( path, os.O_RDWR|os.O_ CREAT|os.O_EXCL , 0700 )
    >>> f = os.fdopen( fd, 'w+b' )
    >>> f[/color][/color][/color]
    <open file '<fdopen>', mode 'w+b' at 0xb7de7a60>[color=blue][color=green][color=darkred]
    >>> print >>f, "Hello"
    >>> f.seek(0)
    >>> f.read()[/color][/color][/color]
    'Hello\n'[color=blue][color=green][color=darkred]
    >>> f.close()[/color][/color][/color]

    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    Comment

    • djw

      #3
      Re: mkstemp on pre-2.3 Python

      Thank you very much, that was the ticket!

      -Don


      Nick Craig-Wood wrote:
      [color=blue]
      > djw <donald.welch@h p.com> wrote:[color=green]
      >> 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.mkstem p # 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_EX CL, 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?[/color]
      >
      > Replace the os.O_WRONLY with os.O_RDWR and all will be fine
      >[color=green][color=darkred]
      >>>> import tempfile, os
      >>>> suffix=""
      >>>> path = tempfile.mktemp ( suffix )
      >>>> fd = os.open( path, os.O_RDWR|os.O_ CREAT|os.O_EXCL , 0700 )
      >>>> f = os.fdopen( fd, 'w+b' )
      >>>> f[/color][/color]
      > <open file '<fdopen>', mode 'w+b' at 0xb7de7a60>[color=green][color=darkred]
      >>>> print >>f, "Hello"
      >>>> f.seek(0)
      >>>> f.read()[/color][/color]
      > 'Hello\n'[color=green][color=darkred]
      >>>> f.close()[/color][/color]
      >[/color]

      Comment

      Working...