Problem writing temporary file using TemporaryFile()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    Problem writing temporary file using TemporaryFile()

    Hi all,

    Basically, i'm trying to create temporary file using TemporaryFile() module.
    Code:
    TemporaryFile( [mode='w+b'[, bufsize=-1[, suffix[, prefix[, dir]]]]])
    It's done perfectly to create an intended file. But, i encountered a problem to write some useful testing text into the temporary file. When i try to used os.open() it always appears "TypeError: coercing to Unicode: need string or buffer, instance found"

    How do i handle this problem??

    What am i supposed to use instead of os.open() ??

    The following code appears to work properly, i used mktemp() module in which it has been deprecated,

    Code:
    		timestamp = str(time())
    		prefix = 'test'
    		suffix = timestamp
    		directory = 'temp'   
    		date = strftime("%a, %d %b %y", localtime())
    		path = tempfile.mktemp(suffix,prefix, directory)
    		fd = os.open( path, os.O_RDWR|os.O_CREAT|os.O_EXCL,0700 )
    		f=os.fdopen(fd,'w+b')
    		post = email.Message.Message()
    		post["Title"] = "This is a testing title"
    		post["Date"] = date
    		post["Timestamp"] = timestamp
    		print >>f, post
    		f.seek(0)
    		f.close()
    Thanks for any advice and help
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Hmm, interesting. I've never seen anyone use >> for writing to file in Python. You have some very strange things in this script... however I cannot reproduce your error. I don't know where email.Message.M essage() is coming from; but that is past the point where you receive an error anyway.

    When I want to open I file I simply do the following:
    [code=python]
    >>> filename = 'foo.bar'
    >>> fh = open(filename, 'w')
    >>> fh.write('This is some text\n')
    >>> fh.close()
    >>>
    >>> fh = open(filename, 'r')
    >>> fh.read()
    'This is some text\n'
    >>> [/code]

    It seems odd that you are receiving errors about unicode. Have you done this interactively in the shell to see if perhaps one of your strings somewhere is accidentally a unicode?

    Comment

    • crystal2005
      New Member
      • Apr 2007
      • 44

      #3
      Originally posted by jlm699
      Hmm, interesting. I've never seen anyone use >> for writing to file in Python. You have some very strange things in this script... however I cannot reproduce your error. I don't know where email.Message.M essage() is coming from; but that is past the point where you receive an error anyway.

      When I want to open I file I simply do the following:
      [code=python]
      >>> filename = 'foo.bar'
      >>> fh = open(filename, 'w')
      >>> fh.write('This is some text\n')
      >>> fh.close()
      >>>
      >>> fh = open(filename, 'r')
      >>> fh.read()
      'This is some text\n'
      >>> [/code]

      It seems odd that you are receiving errors about unicode. Have you done this interactively in the shell to see if perhaps one of your strings somewhere is accidentally a unicode?

      The way i used >> to write file is probably just another way in python. My real problem happened when i tried to write file into a temporary file produced by TemporaryFile module.

      What i have done as the following code

      Code:
      import tempfile, email, os
      from time import time, localtime, strftime
      from email.Message import Message
      
      timestamp = str(time())
      prefix = 'test'
      suffix = timestamp
      directory = 'temp'
      date = strftime("%a, %d %b %y", localtime())
      path = tempfile.TemporaryFile('w+b', -1, suffix, prefix, directory)
      Till this step, so far as i expected. It produced a temporary file that would clean up itself after i closed interactive python shell.

      Now, when i try the following code
      Code:
      fd = os.open( path, os.O_RDWR|os.O_CREAT|os.O_EXCL,0700 )
      it always appears to have unicode error. It didn't appear any error then i used mktemp module.

      I don't know why ....

      any advice??

      Thanks

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        I see what you're saying now...

        TemporaryFile doesn't simply return a filename like the mktemp function does, it actually returns the file handle to the file. If you would like to view the name of the file use: path.name

        path can be used as a regular file handle at this point with the functions write(), seek(), flush(), etc.

        HTH

        Comment

        Working...