Should I close after popen??

Andrew Markebo flognat at flognat.myip.org
Thu Mar 15 18:35:47 EST 2001


/ "Ken Seehof" <kens at sightreader.com> wrote:
| [...]
| In other words, you are better off closing files explicitly.
| 
| In the case of popen, it seems to me like it wouldn't matter if you
| leave a few open streams lying around, but it just seems sloppy,
| like not washing your dishes after dinner.

I agree, but I tried with close, but I got the following problems:

Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/tmp/python-18803PG", line 314, in ?
    mymain()
  File "/usr/tmp/python-18803PG", line 304, in mymain
    keepALive()
  File "/usr/tmp/python-18803PG", line 259, in keepALive
    if PingAway():
  File "/usr/tmp/python-18803PG", line 230, in PingAway
    fd1.close()
IOError: [Errno 10] No child processes

The code is:


    fd1=os.popen("ping -q -c 3 %s 2> /dev/null" % name1)
    fd2=os.popen("ping -q -c 3 %s 2> /dev/null" % name2)
    pid1, res1=os.wait(fd1)
    out1=fd1.read()
    pid2, res2=os.wait(fd2)
    out2=fd2.read()
    fd1.close()
    fd2.close()

        /Andy



More information about the Python-list mailing list