【问题标题】:Python - Trouble getting exception - os.system commandPython - 遇到异常时遇到问题 - os.system 命令
【发布时间】:2018-06-19 14:49:53
【问题描述】:

我收到值为 128 的“未知错误”。这是我尝试过的事情,但我无法进入异常块。

也在我得到的控制台中:

错误:找不到进程“NULL.exe”。

try:
    tmp = os.system("c:/windows/system32/taskkill /f /im NULL.exe")
except OSError as e:
    print("We made it into the excepetion!!" + str(e))

try:
    tmp = os.system("c:/windows/system32/taskkill /f /im NULL.exe")
except OSError:
    print("We made it into the excepetion!!")

try:
    tmp = os.system("c:/windows/system32/taskkill /f /im NULL.exe")
except os.error:
    print("We made it into the excepetion!!")

try:
    tmp = os.system("c:/windows/system32/taskkill /f /im NULL.exe")
except ValueError:
    print("We made it into the excepetion!!")

try:
    tmp = os.system("c:/windows/system32/taskkill /f /im NULL.exe")
except:
    print("We made it into the excepetion!!")

【问题讨论】:

  • 当 tmp 不为 0 时,您可能需要引发 oserror 异常
  • 是的,我可以试试这个,但这会结束我的功能或进程吗??

标签: python try-catch os.system except taskkill


【解决方案1】:

os.system() 在命令失败(或未找到)时不会抛出异常。当您使用错误的参数类型(它需要一个字符串)时,它只会引发异常。如果你真的需要例外,你可以使用subprocess.call()

【讨论】:

  • 谢谢,很高兴知道我会调查此事。是的,我想捕捉错误,因为我正在寻找一个不知道是哪一个的 32 位或 64 位进程,因此很可能会出现异常。我真的在做所有这些以防止控制台中的错误,最坏的情况是它仍然存在......再次感谢您!
  • 能否将我的答案标记为正确?你也可以用“pefile”模块来检查你的exe的属性。
  • FWIW 我试过了,因为我对如何自己解决它感兴趣:import pefile pe = pefile.PE(r"C:\Python36\python.exe", fast_load=True) magic = pe.OPTIONAL_HEADER.Magic if magic == 0x20b: print("64 bit") elif magic == 0x10b: print("32 bit") ...
【解决方案2】:

当您的命令失败时,Python 不会拒绝,它只会捕获您刚刚运行的命令的返回码。 因此,您必须根据返回值进行自定义期望并提高它。 我用你的命令做了一些实验。

这里是错误代码和我发现的含义:

0--任务成功杀死

1--访问被拒绝

128--找不到进程

我的代码解决您的问题:

import os
#Making a custom Expection for ourselfs
class TaskkillError(Exception):
     def __init__(self, value): #value is the string we will return when expection is raised
         self.value = value
     def __str__(self):
         return repr(self.value)


tmp = os.system("c:/windows/system32/taskkill /f /im NULL.exe")
if tmp!=0:
    if tmp==1:
        raise TaskkillError('Acces denided') #Error code=1 Permission Error 
    if tmp==128:
        raise TaskkillError("Process not found") #Error code=128 Process not found
    else:
        raise TaskkillError("UnknowError Error Code returned by taskkill:"+str(tmp))
else:
    print("Task succesfully killed")

【讨论】:

  • 顺便说一句,有人知道这个返回码记录在哪里吗?
  • 谢谢!
【解决方案3】:

好的,我终于想通了,非常感谢您的帮助。我试图使用 subprocess.run、call、check_output、check_call 等。还有 stderr 等的不同参数。我还尝试捕捉我读到的每种类型的错误。每次都向控制台抛出错误。这在我的情况下不起作用,因为我正在寻找 32 位和 64 位进程,因为我知道它们中的一个每次都会失败。

最终我所要做的就是使用 Popen。通过使用 Popen,我基本上可以将错误输入 PIPE,实际上我什至不需要 try/except 块。再次感谢大家的帮助,这里有一个示例代码。

from subprocess import Popen, PIPE

bit32 = True
bit64 = True

p = Popen('c:windows/system32/taskkill /f /im notepad.exe', shell=True, stdout=PIPE, 
stderr=PIPE)
output,error = p.communicate()

if (len(output) == 0):
    print("32 bit not found")
    bit32 = False
if (len(output) > 0):
    print(output)

p = Popen('c:windows/system32/taskkill /f /im notepad64EXAMPLE.exe', shell=True, 
stdout=PIPE, stderr=PIPE)
output,error = p.communicate()

if (len(output) == 0):
   print("64 bit not found")
   bit64 = False
if (len(output) > 0):
   print(output)

if(not bit32 and not bit64):
   print("Could not find 32/64 bit process")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2017-10-09
    • 2021-12-19
    • 1970-01-01
    • 2013-06-13
    • 2017-05-02
    相关资源
    最近更新 更多