Python,子进程,call(),check_call和returncode来查找是否存在命令 [英] Python, subprocess, call(), check_call and returncode to find if a command exists

查看:2772
本文介绍了Python,子进程,call(),check_call和returncode来查找是否存在命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了如何使用call()来获取我的python脚本来运行命令:

I've figured out how to use call() to get my python script to run a command:

import subprocess

mycommandline = ['lumberjack', '-sleep all night', '-work all day']
subprocess.call(mycommandline)

这有效,但有一个问题,如果用户没有伐木工人在他们的命令路径?如果lumberjack放在与python脚本相同的目录,但是脚本如何知道它应该寻找伐木工人呢?我想如果有一个命令没有发现错误,然后lumberjack不会在命令路径,脚本可以尝试找出它的目录是什么,寻找伐木工人在那里,最后警告用户将伐木工人复制到一个那两个地方,如果没有找到在任一个。我如何找到错误消息是什么?我读到check_call()可以返回一个错误消息和一些关于returncode属性。我找不到如何使用check_call()和returncode的例子,消息是什么,或者如何知道消息是否是命令未找到。

This works but there's a problem, what if users don't have lumberjack in their command path? It would work if lumberjack was put in the same directory as the python script, but how does the script know it should look for lumberjack? I figured if there was a command-not-found error then lumberjack wouldn't be in the command path, the script could try to figure out what its directory is and look for lumberjack there and finally warn the user to copy lumberjack into one of those two places if it wasn't found in either one. How do I find out what the error message is? I read that check_call() can return an error message and something about a returncode attribute. I couldn't find examples on how to use check_call() and returncode, what the message would be or how I could tell if the message is command-not-found.

推荐答案

哇,那很快!我结合了Theodros Zelleke的简单例子和steveha使用的函数与关于OSError和Lattyware关于移动文件的注释的abarnert注释:

Wow, that was fast! I combined Theodros Zelleke's simple example and steveha's use of functions with abarnert comment about OSError and Lattyware's comment about moving files:

import os, sys, subprocess

def nameandpath():
    try:
        subprocess.call([os.getcwd() + '/lumberjack']) 
        # change the word lumberjack on the line above to get an error
    except OSError:
        print('\nCould not find lumberjack, please reinstall.\n')
        # if you're using python 2.x, change the () to spaces on the line above

try:
    subprocess.call(['lumberjack'])
    # change the word lumberjack on the line above to get an error
except OSError:
    nameandpath()

它在Mac OS-X(6.8 / Snow Leopard),Debian(Squeeze)和Windows(7)。它似乎工作的方式,我想在所有三个操作系统。我尝试使用check_call和CalledProcessError,但无论我做了什么,我似乎每次都得到一个错误,我不能得到脚本来处理错误。为了测试脚本,我把名字从'lumberjack'改成了'deadparrot',因为我在我的脚本的目录中有了伐木工人。

I tested it on Mac OS-X (6.8/Snow Leopard), Debian (Squeeze) and Windows (7). It seemed to work the way I wanted it to on all three operating systems. I tried using check_call and CalledProcessError but no matter what I did, I seemed to get an error every time and I couldn't get the script to handle the errors. To test the script I changed the name from 'lumberjack' to 'deadparrot', since I had lumberjack in the directory with my script.

你看到这个脚本写的任何问题吗?

Do you see any problems with this script the way it's written?

这篇关于Python,子进程,call(),check_call和returncode来查找是否存在命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆