[Tutor] os.popen

Hugo González Monteverde hugonz-lists at h-lab.net
Mon Nov 29 20:39:06 CET 2004


I would do it, even if the filehandles were automatically closed on 
exit. There's more to it though: the pipes are buffered and there may be 
more data in child_stdout.  What I'd do is empty it before closing, 
though I'm not sure if this is necessary.  Maybe someone can step in and 
clarify a bit, but to sum it up: by all means close the handles...

Probably if this is the output of a command, then EOF has been 
encountered on child_stdout too, so you could do:

tcextract[1].read()

pretty safely, without blocking...


then do:
tcextract[1].close()
tcextract[2].close()
tcextract[0].close() #This one is STDIN...

Hugo

Klas Marteleur wrote:
> hmm..
> Is it nesessary to close the "file objects"?
> 
> a = os.popen3("tcextract -v")
> tcexVerOut= a[2].readlines() 
> a[0].close() #Nesessary?
> a[1].close() #Nesessary?
> a[2].close() #Nesessary?
> 
> Regards Klas
> 
> 
> 
>>Ok i used popen3 instead, reading the "child-stderr" instead and it worked.
>>
>>
>>>>>tcextract = os.popen3("tcextract -v")
>>>>>tcextract[2].readlines()
>>
>>['tcextract (transcode v0.6.12) (C) 2001-2003 Thomas Oestreich\n']
>>
>>Thanks Hugo
>>
>>Regards Klas
>>
>>söndagen den 28 november 2004 19.26 skrev du:
>>
>>>Hi Klas,
>>>
>>>Maybe the output you're trying to get is in STDERR and not STDOUT?? I've
>>>had to deal with this a number of times, so may I wrote a Tkinter script
>>>to let me know what output is to STDERR and what is to STDOUT. os.popen
>>>will open STDOUT if invoked with "r" (which is the default).
>>>
>>>Try looking of other popen* functions. They provide STDOUT + STDERR
>>>(popen4) or separate STDOUT + STDERR (popen3)
>>>
>>>BTW< if you'd like to have my script (the Streamshower, I call it =) )
>>>just email me ...
>>>
>>>Hugo
>>>
>>>Klas Marteleur wrote:
>>>
>>>>Hi
>>>>I have a little problem that is confusing me.
>>>>
>>>>If i write the following in a bash shell i get the desired output:
>>>>[klas at h180n2fls32o849 klas]$ kwrite -v
>>>>Qt: 3.2.3
>>>>KDE: 3.2 BRANCH >= 20040204
>>>>KWrite: 4.2
>>>>
>>>>It also works in a python shell...
>>>>
>>>>
>>>>>>>os.popen("kwrite -v").readlines()
>>>>
>>>>['Qt: 3.2.3\n', 'KDE: 3.2 BRANCH >= 20040204\n', 'KWrite: 4.2\n']
>>>>
>>>>If i try the following in a bash shell, it also workes:
>>>>[klas at h180n2fls32o849 klas]$ tccat -v
>>>>tccat (transcode v0.6.12) (C) 2001-2003 Thomas Oestreich
>>>>
>>>>But if i try the same in a python shell i get:
>>>>
>>>>>>>os.popen("tccat -v").readlines()
>>>>
>>>>[]
>>>>
>>>>Not even if i specify the path it works.
>>>>
>>>>
>>>>>>>os.popen("/usr/bin/tccat -v").readlines()
>>>>
>>>>[]
>>>>
>>>>
>>>>What am i missing?
>>>>
>>>>Kind Regards
>>>>Klas
>>>>_______________________________________________
>>>>Tutor maillist  -  Tutor at python.org
>>>>http://mail.python.org/mailman/listinfo/tutor
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list