0

I am getting 'Unknown Error' with the value of 128. Here are the things I have tried and I cannot manage to get into the exception block.

Also in the console I am getting:

ERROR: The process "NULL.exe" not found.

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!!")
AronAtVW
  • 115
  • 1
  • 2
  • 11

3 Answers3

1

os.system() doesn't throw an exception when the command fails (or isn't found). It just throws an exception when you are using the wrong argument type (it demands a string). If you really need an exception you can use subprocess.call().

ofrommel
  • 2,129
  • 14
  • 19
  • Thank you, good to know I will look into this. Yes, I want to catch the error because I am looking for a 32 or 64 bit process not knowing which one, therefor most likely there will be an exception. I am really doing all of this to prevent the error in the console, worst case it stays... Thank you again! – AronAtVW Jun 19 '18 at 17:38
  • Could you please mark my answer as correct? Also you might something like the "pefile" module to check the attributes of your exe. – ofrommel Jun 20 '18 at 12:09
  • FWIW I tried that because I was interested in how to solve it myself: `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") ...` – ofrommel Jun 22 '18 at 14:23
0

Python won't no when your command failed its only catches the return code from the command you just have ran. So you have to make custom expection and raise it based on the return value. I runned some experiments with your command.

Here is the error codes and there meanings what i found:

0--Task killed succesfully

1--Acces denided

128--Process Not found

My code for your problem:

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")
KJG
  • 31
  • 4
0

Okay guys I figured it out finally, thank you so much for the help. I was trying to use subprocess.run, call, check_output, check_call etc. Also different params for stderr etc. I also tried catching every type of error I read about. Kept throwing errors to the console every time. This wouldn't work in my situation because I am looking for both a 32bit and 64bit process knowing one of them is going to fail every time.

Ultimately all I had to do was use Popen. By using Popen I could basically feed the errors into PIPE and I actually didn't even need a try/except block. Thanks again for all the help, here is a sample code.

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")
AronAtVW
  • 115
  • 1
  • 2
  • 11