what does python 3.6 subprocess.Popen().returncode of 100 means?
looks like you got your 100 returncode from apt-get install command :
E: Unable to locate package ubuntu-restricted-extra
Why does subprocess.run create return code 1 even if the command was successful?
It totally depends on the value returned by your program with different arguments.
For example, when I run the following on my computer (git is installed):
result = subprocess.run(['git'], capture_output=True)
result.returncode
is 1
If I run git with parameter --version like this:
result = subprocess.run(['git','--version'], capture_output=True)
result.returncode
is 0
To really check if a program exists or not (on Windows) you could do something like:
try:
result = subprocess.run(['choco'], capture_output=True)
print(result)
except FileNotFoundError:
print("Program not installed")
Check out this official documentation on Subprocess
Python 'return not' statement in subprocess returncode
not
is a boolean operator that returns the boolean inverse of the value. return
returns the result of that operator. In other words, the expression should be read as return (not self.myReturnCode)
. Quoting the documentation:
The operator
not
yieldsTrue
if its argument is false,False
otherwise.
If self.myReturnCode
is a true value, not self.myReturnCode
is False
, and vice versa. Note that self.myReturnCode
can be any Python value, but not
always returns a boolean value, either True
or False
.
If externalProcessPopen.returncode
is the return code of an external process, then it'll be a positive integer if the process exited with an error, 0
if it exited successfully. This is called the process exit status; what non-zero values are returned depends entirely on the process. not 0
is then True
, not 1
(or a higher integer value) gives you False
.
If it is None
, then True
(not None
is True
) would be returned as well, but a subprocess.Popen()
return code is only None
if the process has not yet exited.
Python error codes
Are you getting the exit status from mysubproc.returncode?
From http://docs.python.org/library/subprocess.html#subprocess.Popen.returncode:
A negative value -N indicates that
the child was terminated by signal N
(Unix only).
Signals 6 & 11 are SIGABRT (abort) and SIGSEGV (segfault) ( http://linux.die.net/man/7/signal ). My guess is that those other scripts are running into badness with an extension or one of the libraries that an extension depends on. You may have a bad build then - either recompile if you did so manually or see if there's an updated package.
Python Forcing Subprocess To Fail and Get A Return Code Of Not Zero
Even if you could get this to work, your users are going to hate you for telling them there are syntax errors in their script, but not what or where they are.
So forget about subprocess and use the built-in compile function instead:
script = 'print(0'
try:
compile(script, '<string>', 'exec')
except SyntaxError as exception:
# do something user-friendly with exception...
else:
# syntax seems okay! run script...
Don't be fooled by the 'exec'
argument: this only specifies the kind of code the script contains. If there are no syntax errors, compile
returns a code object.
How to return status code in Python without actually exiting the process?
Wait until the end of your loop to return the 1 exit code. I'd do it like:
def process_pdf_dir() -> int:
ret = 0
for file in os.listdir('pdf_files/'):
if file.endswith('.pdf'):
if not anonymiser(file):
# remember to return a warning
ret = 1
if criticalError:
# immediately return a failure
return 2
return ret
if __name__ == '__main__':
sys.exit(process_pdf_dir())
If "critical error" includes uncaught exceptions, maybe put that outside of process_pdf_dir
, e.g.:
if __name__ == '__main__':
try:
sys.exit(process_pdf_dir())
except:
sys.exit(2)
Return code of subprocess.run when using shell pipes
This doesn't do what you think:
>>> subprocess.run(['false', '|', 'true'], shell=True)
When you pass a list of strings to subprocess.run
and specify shell=True
, only the first item is interpreted as a command (the remaining items are provided as arguments to the shell). Compare the output of:
>>> subprocess.run('echo hello world', shell=True)
hello world
CompletedProcess(args='echo hello world', returncode=0)
With:
>>> subprocess.run(['echo', 'hello', 'world'], shell=True)
CompletedProcess(args=['echo', 'hello', 'world'], returncode=0)
With:
>>> subprocess.run(['echo $0 $1', 'hello', 'world'], shell=True)
hello world
CompletedProcess(args=['echo $0 $1', 'hello', 'world'], returncode=0)
So what you have there is equivalent to:
>>> subprocess.run('false', shell=True)
And do you know why the return codes of subprocess.run('false | true', shell=True) and subprocess.run(['false', '|', 'true']) are different?
Because the first runs false | true
, which is a pipeline. The exit code of a pipeline is the exit code of the last command in the pipeline (in this case true
). The second call, as discussed here, just runs false
.
Related Topics
Arranging Text Files Side by Side Using Python
Pil Installation Fails Missing:Stdarg.H
Priority of the Logical Operators Not, And, or in Python
How to Mock Requests and the Response
Googletrans Stopped Working with Error 'Nonetype' Object Has No Attribute 'Group'
How to Include Third Party Python Libraries in Google App Engine
Creating a New Corpus with Nltk
Return in Generator Together with Yield
Count Unique Values Per Groups with Pandas
How to Use Python2.7 Pip Instead of Default Pip
Extract Text from Xml Documents in Python
Calling Java/Scala Function from a Task
How to Do Parallel Programming in Python
How to Get the Input from the Tkinter Text Widget
Get Md5 Hash of Big Files in Python