Understanding tempfile.TemporaryFile

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • byte8bits@gmail.com

    Understanding tempfile.TemporaryFile

    Wondering if someone would help me to better understand tempfile. I
    attempt to create a tempfile, write to it, read it, but it is not
    behaving as I expect. Any tips?
    >>x = tempfile.Tempor aryFile()
    >>print x
    <open file '<fdopen>', mode 'w+b' at 0xab364968>
    >>print x.read()
    >>print len(x.read())
    0
    >>x.write("1234 ")
    >>print len(x.read())
    0
    >>x.flush()
    >>print len(x.read())
    0
  • John Machin

    #2
    Re: Understanding tempfile.Tempor aryFile

    On Dec 28, 1:49 pm, byte8b...@gmail .com wrote:
    Wondering if someone would help me to better understand tempfile. I
    attempt to create a tempfile, write to it, read it, but it is not
    behaving as I expect. Any tips?
    >
    >x = tempfile.Tempor aryFile()
    >print x
    >
    <open file '<fdopen>', mode 'w+b' at 0xab364968>
    >
    >print x.read()
    >print len(x.read())
    0
    >x.write("1234" )
    >print len(x.read())
    0
    >x.flush()
    >print len(x.read())
    >
    0
    This is nothing particular to your subject; it applies to all files.

    x.read() starts reading at the CURRENT POSITION, not at the start of
    the file. In all cases above, the then current position was the END of
    the file, so x.read() returned a zero-length string.

    Check out the seek method.

    Comment

    • Shane Geiger

      #3
      Re: Understanding tempfile.Tempor aryFile

      import tempfile
      tmp = tempfile.mktemp ()

      import os
      os.remove(tmp)



      byte8bits@gmail .com wrote:
      Wondering if someone would help me to better understand tempfile. I
      attempt to create a tempfile, write to it, read it, but it is not
      behaving as I expect. Any tips?
      >
      >
      >>>x = tempfile.Tempor aryFile()
      >>>print x
      >>>>
      <open file '<fdopen>', mode 'w+b' at 0xab364968>
      >
      >>>print x.read()
      >>>>
      >
      >
      >>>print len(x.read())
      >>>>
      0
      >
      >>>x.write("123 4")
      >>>print len(x.read())
      >>>>
      0
      >
      >>>x.flush()
      >>>print len(x.read())
      >>>>
      0
      >

      --
      Shane Geiger
      IT Director
      National Council on Economic Education
      sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

      Leading the Campaign for Economic and Financial Literacy

      Comment

      • Steven D'Aprano

        #4
        Re: Understanding tempfile.Tempor aryFile

        On Thu, 27 Dec 2007 18:49:06 -0800, byte8bits wrote:
        Wondering if someone would help me to better understand tempfile. I
        attempt to create a tempfile, write to it, read it, but it is not
        behaving as I expect. Any tips?
        You need to seek to the part of the file you want to read:
        >>x = tempfile.Tempor aryFile()
        >>x.read() # file is empty to start with
        ''
        >>x.write('Nobo dy expects the Spanish Inquisition!')
        >>x.read() # current file is at the end of the file, so nothing to read
        ''
        >>x.seek(0) # move to the beginning of the file
        >>x.read()
        'Nobody expects the Spanish Inquisition!'
        >>x.seek(7)
        >>x.write('EXPE CTS')
        >>x.tell() # where are we?
        14L
        >>x.seek(0)
        >>x.read()
        'Nobody EXPECTS the Spanish Inquisition!'


        --
        Steven

        Comment

        • byte8bits@gmail.com

          #5
          Re: Understanding tempfile.Tempor aryFile

          On Dec 27, 10:12 pm, John Machin <sjmac...@lexic on.netwrote:
          Check out the seek method.
          Ah yes... thank you:
          >>import tempfile
          >>x = tempfile.Tempor aryFile()
          >>x.write("test ")
          >>print x.read()
          >>x.seek(0)
          >>print x.read()
          test


          Comment

          • Steven D'Aprano

            #6
            Re: Understanding tempfile.Tempor aryFile

            On Thu, 27 Dec 2007 21:17:01 -0600, Shane Geiger wrote:
            import tempfile
            tmp = tempfile.mktemp ()
            >
            import os
            os.remove(tmp)
            Not only does that not answer the Original Poster's question, but I don't
            think it does what you seem to think it does.

            >>tmp = tempfile.mktemp ()
            >>tmp
            '/tmp/tmpZkS0Gj'
            >>type(tmp)
            <type 'str'>
            >>import os
            >>os.remove(tmp )
            Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
            OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'




            You might like to read help(tempfile.m ktemp).

            (By the way... the whole point of using tempfile is to avoid needing to
            delete the file by hand afterwards.)



            --
            Steven

            Comment

            • Shane Geiger

              #7
              Re: Understanding tempfile.Tempor aryFile

              Yes, I knew this. Good call, it was just a bad copy and paste example
              of lines that showed up close together in a file. My apologies.
              >import tempfile
              >tmp = tempfile.mktemp ()
              >>
              >import os
              >os.remove(tm p)
              >>
              >
              Not only does that not answer the Original Poster's question, but I don't
              think it does what you seem to think it does.
              >
              >
              >
              >>>tmp = tempfile.mktemp ()
              >>>tmp
              >>>>
              '/tmp/tmpZkS0Gj'
              >
              >>>type(tmp)
              >>>>
              <type 'str'>
              >
              >>>import os
              >>>os.remove(tm p)
              >>>>
              Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
              OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'
              >
              >
              >
              >
              You might like to read help(tempfile.m ktemp).
              >
              (By the way... the whole point of using tempfile is to avoid needing to
              delete the file by hand afterwards.)
              >
              >
              >
              >

              --
              Shane Geiger
              IT Director
              National Council on Economic Education
              sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

              Leading the Campaign for Economic and Financial Literacy

              Comment

              • Karthik Gurusamy

                #8
                Re: Understanding tempfile.Tempor aryFile

                On Dec 27, 7:36 pm, Steven D'Aprano
                <ste...@REMOVE. THIS.cybersourc e.com.auwrote:
                On Thu, 27 Dec 2007 21:17:01 -0600, Shane Geiger wrote:
                import tempfile
                tmp = tempfile.mktemp ()
                >
                import os
                os.remove(tmp)
                >
                Not only does that not answer the Original Poster's question, but I don't
                think it does what you seem to think it does.
                >
                >tmp = tempfile.mktemp ()
                >tmp
                '/tmp/tmpZkS0Gj'
                >type(tmp)
                <type 'str'>
                >import os
                >os.remove(tm p)
                >
                Traceback (most recent call last):
                File "<stdin>", line 1, in <module>
                OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'
                >
                You might like to read help(tempfile.m ktemp).
                >
                (By the way... the whole point of using tempfile is to avoid needing to
                delete the file by hand afterwards.)
                FWIW tempfile.mkstem p needs explicit user deletion. And
                tempfile.mkstem p is recommended over tempfile.mktemp due to security
                reasons.

                Help on function mkstemp in module tempfile:

                mkstemp(suffix= '', prefix='tmp', dir=None, text=False)
                mkstemp([suffix, [prefix, [dir, [text]]]])
                User-callable function to create and return a unique temporary
                file. The return value is a pair (fd, name) where fd is the
                file descriptor returned by os.open, and name is the filename.

                If 'suffix' is specified, the file name will end with that suffix,
                otherwise there will be no suffix.

                If 'prefix' is specified, the file name will begin with that
                prefix,
                otherwise a default prefix is used.

                If 'dir' is specified, the file will be created in that directory,
                otherwise a default directory is used.

                If 'text' is specified and true, the file is opened in text
                mode. Else (the default) the file is opened in binary mode. On
                some operating systems, this makes no difference.

                The file is readable and writable only by the creating user ID.
                If the operating system uses permission bits to indicate whether a
                file is executable, the file is executable by no one. The file
                descriptor is not inherited by children of this process.

                Caller is responsible for deleting the file when done with it.
                <-------
                >>>

                Karthik
                >
                --
                Steven

                Comment

                Working...