用于检查系统调用结果的Pythonic样式
我已经继承了一个基本上是Shell脚本的Python脚本,它使用OS.System()
的重复调用来运行一堆命令行实用程序,而无需检查结果。为了使脚本更加坚固,我正在考虑添加类似下面的类方法:
def subprocess(self, cmd):
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while proc.poll() is None:
if proc.wait() == 0:
return True
else:
return False
def check_package_installed(self, pkg):
cmd = " ".join(["dnf list installed", pkg])
rv = self.subprocess(cmd)
if rv != True:
raise Exception("Package {} not installed.".format(pkg)))
然后,在脚本的主要部分中,类似的是:
try:
checker.check_package_installed("jdkasjdsa")
except Exception as e:
print("Error: ",e)
# recovery code here
这是一种惯用代码任务?
I have inherited a python script that is essentially a shell script, it uses repeated calls to os.system()
to run a bunch of command line utilities without necessarily checking the result. In order to make the script a little more rugged, I was thinking of adding class methods like below:
def subprocess(self, cmd):
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while proc.poll() is None:
if proc.wait() == 0:
return True
else:
return False
def check_package_installed(self, pkg):
cmd = " ".join(["dnf list installed", pkg])
rv = self.subprocess(cmd)
if rv != True:
raise Exception("Package {} not installed.".format(pkg)))
And then, in the main section of the script, something like this:
try:
checker.check_package_installed("jdkasjdsa")
except Exception as e:
print("Error: ",e)
# recovery code here
Is this the kind of idiomatic code another Python programmer would expect for this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
异常
有点宽。让我们假设您的
pkg
将是整数。然后,加入
操作将使用typeerror
失败。但是typeError
是异常
的子类,这意味着您将尝试在此处运行恢复代码
,即使不同的异常
提高看起来您为内部用法创建脚本,这不是一个大问题,但是通常将传递给命令的参数
您的命令结果检查逻辑逻辑很尴尬而脆弱(最重要的问题)
您可以尝试以下代码:
Using of
Exception
is little bit wide.Let's suppose that your
pkg
will be integer. Thenjoin
operation will be failed withTypeError
. HoweverTypeError
is subclass forException
and that means that you will try to runrecovery code here
even if differentException
raisedLooks like you creating script for internal usage and that is not a big problem, but generally arguments passed to command should be escaped
Your command result check logic is awkward and fragile (most important problem)
You may try this code:
我的答案与 @juanpa.arrivillaga评论相似。您可以使用以下示例使用subprocess.run命令,而不是使用popen。 subprocess.run将等待命令完成,并将返回完整的程序实例。如果您不关心输出,则可以使用Pass Check = true,并且如果命令返回错误代码!= 0,则可以提高子过程。
输出
请阅读您在系统上使用的Python版本的subprocess.run文档。
My answer was on similar lines as @juanpa.arrivillaga comment. Instead of using Popen you can use subprocess.run command using the below example. The subprocess.run will wait for the command to complete and it will return a CompletedProcess instance. If you dont care output you can use pass check=True and it will raise subprocess.CalledProcessError exception if the command returns error code !=0.
Output
Please read the subprocess.run documentation specific to Python version that you are using on your system.