Creating a temporary file in Python

Diez B. Roggisch deets at nospam.web.de
Wed Oct 31 14:19:41 CET 2007


looping wrote:

> On Oct 31, 2:16 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> 
>> 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



More information about the Python-list mailing list