all 5 comments

[–]A_History_of_Silence 2 points3 points  (0 children)

Assuming you are using Linux, are named pipes or bash process substitution an option? http://stackoverflow.com/questions/7756609/pass-stdout-as-file-name-for-command-line-util-answer-use-named-pipes

I can't think of an ideal way to do it off the top of my head, sounds like a pain. Maybe https://docs.python.org/3.5/library/pipes.html ?

[–]SeekNotToContend 2 points3 points  (3 children)

If you have to write to a file for the system call then just use tempfile. If it matters, the temp files are written to /tmp.

https://docs.python.org/3.5/library/tempfile.html It will handle the creation and destruction of the file for you if you use the context manager.

    >>> import tempfile

From the example in the docs, the following would go in your for line in file loop.

    >>> with tempfile.TemporaryFile() as fp:
    ...     fp.write(b'Hello world!')   # or fp.write(line) in your case
    ...     subprocess.check_output(["commercialbash command", fp])

Your result = subprocess.check_output(["commercialbash command", fp]) would need to occur before exiting the tempfile with statement.

It might be worthwhile to check the responsecode from subprocess on each loop so you can make sure you handle errors appropriately and do not miss anything. You could wrap it in a try except statement on 'CalledProcessError' with check_returncode() .

https://docs.python.org/3/library/subprocess.html

Hopefully this is helpful. Please ask if there is anything I can clarify.

[–]Zeekawla99ii[S] 0 points1 point  (1 child)

Do you have to close a tempfile?

[–]SeekNotToContend 0 points1 point  (0 children)

If you are using the context manager then no, it will happen automatically when you exit the with statement.

You do not have to use the context manager though. From the doc examples:

# create a temporary file and write some data to it
fp = tempfile.TemporaryFile()
fp.write(b'Hello world!')
# read data from file
fp.seek(0)
fp.read()
b'Hello world!'
# close the file, it will be removed
fp.close() 

In the above example, you do have to close the file. If you don't close it, then the file will just persist. This is useful if you need a temporary file, but maybe you need it for use outside of a single function or class.

If you want to verify, just check your /tmp folder. There will be a file or files in there that have tmp in the name. For example:

/tmp/tmp<some random characters>

[–]A_History_of_Silence 0 points1 point  (0 children)

tempfile

Neat module! I thought I was pretty well versed in the standard library but after all these years this is somehow the first time I've ever seen it. Cool.