Creating a temporary file in Python

looping kadeko at gmail.com
Wed Oct 31 13:48:18 CET 2007


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.




More information about the Python-list mailing list