views:

108

answers:

2

Hi all.

From my understanding, os.popen() opens a pipe within Python and initiates a new sub process. I have a problem when I run a for loop in conjunction with os.popen(). I can't seem to CTRL+C out of the loop. Here is my code:

for FILE in os.popen("ls $MY_DIR/"):
    os.system("./processFile " + FILE)

Whenever I try to CTRL+C, Python will stop the ./processFile program but NOT the python program itself!

I have Google'd around and couldn't seem to find the correct answer. Some people recommend using SIGNALS (I tried... it didn't work). Another tried to use PIDs and killing child PIDs but I couldn't seem to get it.

Can someone lead me to a better example so I can stop the programming when I use CTRL+C (SIGINT) ?

+2  A: 

The behavior is correct. Ctrl+C stops the foreground process and not its parent process. Calling the shell and using ls is inappropriate here, your code should better be written as follows (untested):

import os
import subprocess
for fname in os.listdir(directory):
    path = os.path.join(directory, fname)
    subprocess.check_call(["./processFile", path])
Philipp
Thanks for the response! I try to call subprocess.check_call but now I'm getting this error:AttributeError: 'module' object has no attribute 'check_call'I'm trying to google the results to no avail.
Carlo del Mundo
>>> for fname in os.listdir( os.environ['PSIM_DATA']):<br>... path = os.path.join( os.environ['PSIM_DATA'], fname)<br>... subprocess.check_call(["echo", path])<br><br><br>Gives me the error
Carlo del Mundo
+4  A: 

I see some answer correctly recommended subprocess.check_call and the OP in a comment said

I'm getting this error: AttributeError: 'module' object has no attribute 'check_call'

Per the docs I just linked to, check_call is marked as:

New in version 2.5.

so it looks like the OP is using some ancient version of Python -- 2.4 or earlier -- without mentioning the fact (the current production-ready version is 2.7, and 2.4 is many years old).

The best one can recommend, therefore, is to upgrade! If 2.7 is "too new" for your tastes (as it might be considered in a conservative "shop"), 2.6's latest microrelease should at least be fine -- and it won't just give you subprocess.check_call, but also many additional feautures, bug fixes, and optimizations!-)

Alex Martelli
Wow! thanks I'll update now!
Carlo del Mundo