开发者

check for os.system() reply

I like using the system() command from the os module. Is there a way to check if the command gives me a reply or not ? I have code to terminate the command if it doesn't finis开发者_StackOverflowh in x seconds but I want to improve it to terminate it if it does not reply in x seconds.

thanks !


This depends what you mean with "reply". os.system() returns the exit status of the command (which is an int, typically 0 for success). If this is not enough, have a look at the subprocess module.


You didn't say what OS you are running on. Alas, it is different for Windows and Linux. Here's what I do:

import os

try:
    WEXITSTATUS = os.WEXITSTATUS
except AttributeError: # running on Windows
    def WEXITSTATUS(arg):
        return arg
    os.environ["HOME"] = os.environ["USERPROFILE"]


if WEXITSTATUS(os.system(cmd)) != 0:
    pass # bad return value
else:
    pass # good return value
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜