开发者

Python - Using os.popen() to parse Unix "ls" - Problems with Killing Child Process

开发者 https://www.devze.com 2023-01-09 11:06 出处:网络
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 lo

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) ?


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!-)


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])
0

精彩评论

暂无评论...
验证码 换一张
取 消