Creating a temporary file in Python

Discussion in 'Python' started by looping, Oct 31, 2007.

  1. looping

    looping Guest

    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.
     
    looping, Oct 31, 2007
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Oct 31, 2007
    #2
    1. Advertising

  3. looping

    looping Guest

    On Oct 31, 2:16 pm, "Diez B. Roggisch" <> 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.
     
    looping, Oct 31, 2007
    #3
  4. looping <> 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 -- -- 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
     
    Sion Arrowsmith, Oct 31, 2007
    #4
  5. looping wrote:

    > On Oct 31, 2:16 pm, "Diez B. Roggisch" <> 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
     
    Diez B. Roggisch, Oct 31, 2007
    #5
  6. looping

    looping Guest

    On Oct 31, 2:16 pm, Sion Arrowsmith <>
    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)
     
    looping, Oct 31, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sudha
    Replies:
    1
    Views:
    317
  2. Jerry Spence1

    Creating temporary database on user's PC.

    Jerry Spence1, Dec 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    425
    Jerry Spence1
    Dec 28, 2004
  3. Denis Remezov
    Replies:
    23
    Views:
    680
    Benoit Mathieu
    Apr 17, 2004
  4. Replies:
    7
    Views:
    3,326
    James Kanze
    Feb 12, 2008
  5. Replies:
    2
    Views:
    566
    Lawrence D'Oliveiro
    Jul 17, 2008
Loading...

Share This Page