In this discussion about the easiest way to start a process and discard its output, I suggested the following code:
with open('/dev/null', 'w') as dev_null: subprocess.call(['command'], stdout=dev_null, stderr=dev_null)
Another developer suggested this version:
subprocess.call(['command'], stdout=open('/dev/null', 'w'), stderr=STDOUT)
The C ++ programmer in me wants to say that when objects are released, this is an implementation detail, therefore, to avoid leaving the file descriptor for an indefinite period of time, I have to use with . But a couple of resources suggests that Python always or almost always uses reference counting for code in this case, in which case the file descriptor should be restored as soon as subprocess.call is complete, and using with not required.
(I think leaving the descriptor file open for /dev/null , in particular, hardly matters, so pretend to be an important file.)
Which approach is best?
source share