Should I close after popen??

David Allen mda at idatar.com
Sat Mar 17 13:45:31 EST 2001


In article <m38zm6r8ih.fsf at master.athome>, "Andrew Markebo"
<flognat at flognat.myip.org> wrote:

> I am hacking away a small background program, using popen to call commands,
> should I close the file in some way when done or it is taken care of
> automagically??
> 
> I do something like this once every 10 mins..
> 
>         fd1=os.popen("command") fd2=os.popen("command") os.wait(fd1)
>         os.wait(fd2) values=fd1.read()+fd2.read()
> 
> And.. anything to worry about, nesting popen/wait like this??
> (Linux/Solaris)

IIRC when garbage collected, all file handles are 
closed automagically by python.

But that doesn't mean that it's OK to do it, that's
just for safety.  You should ALWAYS close your own
handles, so you can keep track of where they're good
and where they're no good.  You're not going to really
screw things up if you don't explicitly close them
but it's a better idea to do it yourself.  It's good
practice.

As for doing two popen()'s concurrently, that shouldn't
be a problem as long as you're in an environment where
forking is kosher, since popen() does a fork.  From
the C popen() call manpage:  (Python pretty much just
wraps the C interface, I think):

The  popen()  function opens a process by creating a pipe,
forking, and invoking the shell.

-- 
David Allen
http://opop.nols.com/
----------------------------------------
Be wary of strong drink. It can make you shoot at tax collectors and 
miss 
- Lazarus Long, "Time Enough for Love"



More information about the Python-list mailing list