473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problem writing temporary file using TemporaryFile()

crystal2005
Hi all,

Basically, i'm trying to create temporary file using TemporaryFile() module.
Expand|Select|Wrap|Line Numbers
  1. 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,

Expand|Select|Wrap|Line Numbers
  1.         timestamp = str(time())
  2.         prefix = 'test'
  3.         suffix = timestamp
  4.         directory = 'temp'   
  5.         date = strftime("%a, %d %b %y", localtime())
  6.         path = tempfile.mktemp(suffix,prefix, directory)
  7.         fd = os.open( path, os.O_RDWR|os.O_CREAT|os.O_EXCL,0700 )
  8.         f=os.fdopen(fd,'w+b')
  9.         post = email.Message.Message()
  10.         post["Title"] = "This is a testing title"
  11.         post["Date"] = date
  12.         post["Timestamp"] = timestamp
  13.         print >>f, post
  14.         f.seek(0)
  15.         f.close()
  16.  
Thanks for any advice and help
May 29 '08 #1
3 2950
jlm699
314 100+
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.Message() 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:
Expand|Select|Wrap|Line Numbers
  1. >>> filename = 'foo.bar'
  2. >>> fh = open(filename, 'w')
  3. >>> fh.write('This is some text\n')
  4. >>> fh.close()
  5. >>> 
  6. >>> fh = open(filename, 'r')
  7. >>> fh.read()
  8. 'This is some text\n'
  9. >>> 
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?
May 29 '08 #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.Message() 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:
Expand|Select|Wrap|Line Numbers
  1. >>> filename = 'foo.bar'
  2. >>> fh = open(filename, 'w')
  3. >>> fh.write('This is some text\n')
  4. >>> fh.close()
  5. >>> 
  6. >>> fh = open(filename, 'r')
  7. >>> fh.read()
  8. 'This is some text\n'
  9. >>> 
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

Expand|Select|Wrap|Line Numbers
  1. import tempfile, email, os
  2. from time import time, localtime, strftime
  3. from email.Message import Message
  4.  
  5. timestamp = str(time())
  6. prefix = 'test'
  7. suffix = timestamp
  8. directory = 'temp'
  9. date = strftime("%a, %d %b %y", localtime())
  10. path = tempfile.TemporaryFile('w+b', -1, suffix, prefix, directory)
  11.  
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
Expand|Select|Wrap|Line Numbers
  1. fd = os.open( path, os.O_RDWR|os.O_CREAT|os.O_EXCL,0700 )
  2.  
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
May 30 '08 #3
jlm699
314 100+
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
May 30 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
1
by: Frank Lopez | last post by:
In .NET and C#, is there anyway to create a hook for a specific directory or file where my C# .NET software can monitor which static web files are requested by the browser? What I am doing is...
6
by: Dakkar | last post by:
I have a txt file like this ClientFirstLaunch=no NextLoginKey=255 LastServerID=1 PlayerName=Dakkar DefaultChar=0 LastTip=3
1
by: Lauchlan M | last post by:
Hi. I have an ASP.net project that works fine. I copied it to another project using the following steps: << (i) create a new VS.NET ASP.NET project.
1
by: abh1508 | last post by:
Following a release of code the following problem occurs on certain asp ..net pages. This is not a problem on other testing/demo environments. IIS seems to be creating certain files twice in the...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
12
by: Steven Berkovitz | last post by:
I have several ASP.NET applications with near identical web.config files. In one of them I am successfuly able to bind to an assembly while on others I am not. All of them have the following...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.