Hi all,
Basically, i'm trying to create temporary file using TemporaryFile() module.
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,
Thanks for any advice and help
Basically, i'm trying to create temporary file using TemporaryFile() module.
Code:
TemporaryFile( [mode='w+b'[, bufsize=-1[, suffix[, prefix[, dir]]]]])
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()
Comment