2

how to use subprocess if my temp-file argument is in the middle of the command? For example a terminal command looks like this:

program subprogram -a -b tmpFILE otherFILE

I tried variations of this:

from subprocess import Popen, PIPE
from tempfile import SpooledTemporaryFile as tempfile
tmpFILE=tempfile()
tmpFILE.write(someList)
tmpFILE.seek(0)
print Popen(['program','subprogram', '-a', '-b', otherFile],stdout=PIPE,stdin=tmpFILE).stdout.read()
f.close()

or

print Popen(['program','subprogram', '-a', '-b', tmpFILE, otherFile],stdout=PIPE,stdin=tmpFILE).stdout.read()

but nothing works... My temporary generated file in python shouldn't be as the last parameter.

Thanks

Thedam
  • 51
  • 1
  • 7

1 Answers1

2

Is there a reason to use SpooledTemporaryFile instead of other types of temp file? If not, I recommend using NamedTemporaryFile as you can retrieve the name from it. I have tried to retrieve the name from SpooledTemporaryFile and got '<fdopen>' which does not seem to be valid.

Here is the suggested code:

from subprocess import Popen, PIPE
import tempfile

with tempfile.NamedTemporaryFile() as temp_file:
    temp_file.write(someList)
    temp_file.flush()
    process = Popen(['program', 'subprogram', '-a', '-b', temp_file.name, otherFile], stdout=PIPE, stderr=PIPE)
    stdout, stderr = process.communicate()

Discussion

  • Using the with statement, you don't have to worry about closing the file. As soon as the with block is finished, the file is automatically closed.
  • Instead of calling seek, you should call flush to commit your file buffer to disk before calling program.
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • both `.seek(0)` and `.flush()` should pass the data to OS (so that `program` could read it back). As an alternative a [named pipe or `/dev/fd/#` filenames could be used](http://stackoverflow.com/a/28840955/4279) (to avoid writing all at once or to avoid writing the data to disk). – jfs Jun 01 '16 at 12:29
  • From the [docs](https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile), it is not guaranteed that you can open a `NamedTemporaryFile` again. – ktb Feb 22 '21 at 16:40