Creating a temporary file in Python

Diez B. Roggisch deets at nospam.web.de
Wed Oct 31 13:16:10 CET 2007


looping wrote:

> 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



More information about the Python-list mailing list