简体   繁体   English

检查os.system()答复

[英]check for os.system() reply

I like using the system() command from the os module. 我喜欢使用os模块中的system()命令。 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 finish in x seconds but I want to improve it to terminate it if it does not reply in x seconds. 如果没有在x秒内完成命令,我有终止该命令的代码,但是如果它在x秒内没有答复,我想改进它以终止它。

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). os.system()返回命令的退出状态(它是一个int,成功则通常为0)。 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. Windows,Windows和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM