Creating a temporary file in Python

L

looping

Hi,

I want to create a temporary file, read it in an external command and
finally delete it (in Windows XP).

I try to use tempfile module but it doesn't work, the file couldn't be
open by my other process (error like: SP2-0310: unable to open file "c:
\docume~1\looping\locals~1\temp\tmpau81-s.sql")
Is there a way to make it work or I have to manually manage
everything ?

My non working code:

f = tempfile.NamedTemporaryFile(suffix='.sql')
f.write(txt)
f.flush()
p = subprocess.Popen([SQL_PLUS, '-s', dsn, '@', SQL_PLUS_SCRIPT,
f.name],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
f.close()

Thanks for your help.
 
D

Diez B. Roggisch

looping said:
Hi,

I want to create a temporary file, read it in an external command and
finally delete it (in Windows XP).

I try to use tempfile module but it doesn't work, the file couldn't be
open by my other process (error like: SP2-0310: unable to open file "c:
\docume~1\looping\locals~1\temp\tmpau81-s.sql")
Is there a way to make it work or I have to manually manage
everything ?

My non working code:

f = tempfile.NamedTemporaryFile(suffix='.sql')
f.write(txt)
f.flush()
p = subprocess.Popen([SQL_PLUS, '-s', dsn, '@', SQL_PLUS_SCRIPT,
f.name],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()
f.close()

I'm not an expert, but I think you need to close the file first - you under
windows here, which can be picky about such stuff AFAIK. Or maybe there is
some other mode-specifier.

Diez
 
L

looping

I'm not an expert, but I think you need to close the file first - you under
windows here, which can be picky about such stuff AFAIK. Or maybe there is
some other mode-specifier.

Diez

Actually closing the file delete it without any chance to use it...

Well I changed my code this way:

filename = tempfile.mktemp(suffix='.sql')
f = open(filename, 'wb')
try:
f.write(txt.encode('cp1252'))
f.close()
p = Popen([SQL_PLUS, '-s', dsn,
'@', SQL_PLUS_SCRIPT, f.name],
stdout=PIPE, stderr=STDOUT)
p.wait()
finally:
os.remove(filename)

I understand the security issues of temporary file (as explained in
Python doc) but maybe standard lib need a NamedTemporaryFile that
could be used by another process.
 
S

Sion Arrowsmith

looping said:
I want to create a temporary file, read it in an external command and
finally delete it (in Windows XP).

I try to use tempfile module but it doesn't work, the file couldn't be
open by my other process (error like: SP2-0310: unable to open file "c:
\docume~1\looping\locals~1\temp\tmpau81-s.sql")

You're using NamedTemporaryFile. The tempfile documentation
(http://docs.python.org/lib/module-tempfile.html) says:

" [ ... ] Whether the name can be used to open the file a second time,
while the named temporary file is still open, varies across platforms
(it can be so used on Unix; it cannot on Windows NT or later)."

You probably want to use tempfile.mkstemp and explicitly close
(before running the external command) and delete it.
 
D

Diez B. Roggisch

looping said:
I'm not an expert, but I think you need to close the file first - you
under windows here, which can be picky about such stuff AFAIK. Or maybe
there is some other mode-specifier.

Diez

Actually closing the file delete it without any chance to use it...

Well I changed my code this way:

filename = tempfile.mktemp(suffix='.sql')
f = open(filename, 'wb')
try:
f.write(txt.encode('cp1252'))
f.close()
p = Popen([SQL_PLUS, '-s', dsn,
'@', SQL_PLUS_SCRIPT, f.name],
stdout=PIPE, stderr=STDOUT)
p.wait()
finally:
os.remove(filename)

I understand the security issues of temporary file (as explained in
Python doc) but maybe standard lib need a NamedTemporaryFile that
could be used by another process.

As I said: that most likely is a limitation of your operating system, not
Python.

Diez
 
L

looping

" [ ... ] Whether the name can be used to open the file a second time,
while the named temporary file is still open, varies across platforms
(it can be so used on Unix; it cannot on Windows NT or later)."

I didn't notice this limitation when reading the doc, thanks to point
me to it.

So for the future newbie that look something like this, here is my
final code:

fd, filename = tempfile.mkstemp(suffix='.sql')
f = os.fdopen(fd, 'wb')
try:
f.write(txt.encode('cp1252'))
f.close()
p = Popen([SQL_PLUS, '-s', dsn,
'@', SQL_PLUS_SCRIPT, filename],
stdout=PIPE, stderr=STDOUT)
p.wait()
finally:
os.remove(filename)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top