Reputation:
This is not an exact copy of my code, but it's the only idea I have regarding going about this:
import os
import re
while True:
com = input()
if com == "break":#Doesn't matter
break
run = os.system(com)
if run == "'" + com + "' is not recognized as an internal or external command, operable program or batch file.":
print("Error.")
else:
os.system(com)
And it doesn't work. I simply need a way to know if there's a way to test whether an os.system() function works, print the response if it does, and print "Error." otherwise. If it matters, I'm using python 3.9
Upvotes: -1
Views: 2460
Reputation: 2093
The return of the os.system
function is the return the operating system program (or command) does. Conventionally, it is 0
in case of success and a a number different from 0
if it fails. The error message that you want is not the result of the command, but what is sent to stderr
(typically).
Thus, your code should be:
import os
import re
while True:
com = input()
if com == "break":#Doesn't matter
break
ret_value = os.system(com)
if ret_value != 0:
print(f"Error. Command returned {ret_value}")
else:
print("Command returned success")
Upvotes: 2