Re: Creating a temporary file in Python
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a temporary file in Python
On Oct 31, 2:16 pm, Diez B. Roggisch [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a temporary file in Python
looping wrote: On Oct 31, 2:16 pm, Diez B. Roggisch [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a temporary file in Python
looping [EMAIL PROTECTED] wrote: 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. -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ Frankly I have no feelings towards penguins one way or the other -- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a temporary file in Python
On Oct 31, 2:16 pm, Sion Arrowsmith [EMAIL PROTECTED] wrote: [ ... ] 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) -- http://mail.python.org/mailman/listinfo/python-list