Python script output in file

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Mar 18 19:16:59 EDT 2015


On Thu, 19 Mar 2015 07:22 am, Albert-Jan Roskam wrote:

> 
> ----------------------------
> On Wed, Mar 18, 2015 7:06 PM CET Rustom Mody wrote:
> 
>>On Wednesday, March 18, 2015 at 8:12:12 PM UTC+5:30, Robert Clove wrote:
>>> ./my_eth_script.pl eth0 M > a.txt
>>> 
>>> How can i run this command with subprocess.popen
>>
>>Something like this I guess?
>>
>>>> proc = Popen("cat", shell=True, stdout=open(inname, "w"),
>>>> stdin=open(outname,"r"))
> 
> 
> How will the file object associated with in name be closed? (When is
> close() method called?

Depends on the interpreter.

CPython (the one you are probably using) will notice immediately the proc
object goes out of scope and is garbage-collected. That will free up the
Popen stdout argument, which will garbage collect the file object, which
will close the inname file immediately.

Assuming there are no circular references involved. If there are, then the
file won't be closed until the second garbage collector gets to run. It
will break the circular reference and close the file, but there is no
promise as to how often it will run. The user can configure how often it
runs and even whether it runs at all.

On the other hand, Jython will garbage-collect the file object, but the file
itself may remain open until your code exits, and only be closed on
interpreter-shutdown. Since the operator system probably has limits on how
many files you can have open at once, you might actually run out.

For quick and dirty scripts, it doesn't matter, but best practice is to
explicitly close your files as soon as possible, preferably with a `with`
statement:


# I think inname and outname are reversed...
with open(inname, "w") as outfile, open(outname, "r") as infile:
    proc = Popen("cat", shell=True, stdout=outfile, stdin=infile)
    # do stuff with proc
    # when finished, save the stuff you care about
    result = ...

print(result)


Outside of the `with` block, proc's stdout and stdin will be automatically
closed, even if the block exits with an exception.



-- 
Steven




More information about the Python-list mailing list