Python Return Error Code From Main
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company
Python Script Return Value To Shell
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions python exit code 2 Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million python exception exit code programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Best way to return a value from a python script up vote 19 down vote favorite 12 I wrote a script
Python Sys.exit Example
in python that takes a few files, runs a few tests and counts the number of total_bugs while writing new files with information for each (bugs+more). To take a couple files from current working directory: myscript.py -i input_name1 input_name2 When that job is done, I'd like the script to 'return total_bugs' but I'm not sure on the best way to implement this. Currently, the script prints stuff like: [working directory] [files being opened] [completed
Python Get Exit Code
work for file a + num_of_bugs_for_a] [completed work for file b + num_of_bugs_for_b] ... [work complete] A bit of help (notes/tips/code examples) could be helpful here. Btw, this needs to work for windows and unix. python return return-value share|improve this question asked Aug 14 '13 at 12:11 ofer.sheffer 1,1542819 5 It's generally a bad idea to try to use the return value of an executable to return anything but an error code or 0 for success. What are you going to do with this value when it's returned? –geoffspear Aug 14 '13 at 12:13 1 Don't know about windows, but in linux it is common for programs to output the result to stdout. It seems like normally your script prints a bunch of information, but perhaps with a different flag (maybe c for count?), it just prints the total count of files, e.g. myscript.py -c -i input_name1 input_name2 –arghbleargh Aug 14 '13 at 12:18 @ Wooble, it is a script for finding bugs in report files. The value allows to estimate how well the report files are written. –ofer.sheffer Aug 14 '13 at 13:49 @arghbleargh, I decided to go with an extra bug report file but, pending on what my supervisor decides, I might change it to something like your suggestion. Thanks. –ofer.
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring python return value from script developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Python Exit With Error Message
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join python exit code 255 them; it only takes a minute: Sign up get the exit code for python program up vote 5 down vote favorite 2 I'm running a python program on windowsXP, how can i obtain the exit code after my program ends http://stackoverflow.com/questions/18231415/best-way-to-return-a-value-from-a-python-script ? Thanks. python share|improve this question asked Sep 29 '09 at 10:40 phoenix24 1,3051123 how are you planning to do this? –SilentGhost Sep 29 '09 at 10:45 1 @phoenix24 you should go back to questions you have previously asked and mark the right ones as answered. Your acceptance rating is quite low. 44% –Spooks Oct 4 '11 at 14:25 add a comment| 5 Answers 5 active oldest votes up vote 5 down vote accepted From a http://stackoverflow.com/questions/1491796/get-the-exit-code-for-python-program Windows command line you can use: echo %ERRORLEVEL% For example: C:\work>python helloworld.py Hello World! C:\work>echo %ERRORLEVEL% 0 share|improve this answer answered Sep 29 '09 at 10:47 Dave Webb 125k36249269 add a comment| up vote 4 down vote How do you run the program? Exit in python with sys.exit(1) If you're in CMD or a BAT file you can access the variable %ERRORLEVEL% to obtain the exit code. For example (batch file): IF ERRORLEVEL 1 GOTO LABEL share|improve this answer answered Sep 29 '09 at 10:51 Tarnschaf 2,46211627 2 IF ERRORLEVEL 1 will match exit codes of >= 1 - support.microsoft.com/kb/69576 –David Sykes Sep 29 '09 at 10:55 add a comment| up vote 3 down vote You can also use python to start your python-program import subprocess import sys retcode = subprocess.call([sys.executable, "myscript.py"]) print retcode share|improve this answer edited Sep 29 '09 at 11:15 nosklo 109k33216228 answered Sep 29 '09 at 10:57 Blauohr 4,36711627 add a comment| up vote 1 down vote If you want to use ERRORLEVEL (as opposed to %ERRORLEVEL%) to check for a specific exit value use IF ERRORLEVEL
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the http://stackoverflow.com/questions/1630706/best-practice-in-python-for-return-value-on-error-vs-success workings and policies of this site About Us Learn more about https://bytes.com/topic/python/answers/23611-sys-exit Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. exit code Join them; it only takes a minute: Sign up Best practice in python for return value on error vs. success up vote 27 down vote favorite 10 In general, let's say you have a method like the below. def intersect_two_lists(self, list1, list2): if not list1: self.trap_error("union_two_lists: list1 must not be empty.") return False if not list2: self.trap_error("union_two_lists: list2 must python exit code not be empty.") return False #http://bytes.com/topic/python/answers/19083-standard return filter(lambda x:x in list1,list2) In this particular method when errors are found, I would not want to return the empty list in this case because that could have been the real answer to this specific method call, I want to return something to indicate the parameters were incorrect. So I returned False on error in this case, and a list otherwise (empty or not). My question is, what is the best practice in areas like this, and not just for lists?Return whatever the heck I want and make sure I document it for a user to read? :-) What do most of you folks do: If on success you were supposed to return True or False and you catch an error? If on success you were supposed to return a list and you catch an error? If on success you were supposed to return a file handle and you catch an error? et cetera python return share|improve this question edited Jun 22 '15 at 15:02 Bhargav Rao 23.5k1
solutions from a community of 418,626 IT Pros & Developers. It's quick & easy. sys.exit() P: n/a Ivan Voras In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke Jul 18 '05 #1 Post Reply Share this Question 12 Replies P: n/a Duncan Booth "Ivan Voras"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure? Jul 18 '05 #2 P: n/a Peter Hansen Ivan Voras wrote: In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) sys.exit() is the proper, defined, cross-platform way to exit from a program and return a value to the calling program. Change your definition of elegant and you could consider it easily the most elegant of all solutions. ;-) -Peter Jul 18 '05 #3 P: n/a Ivan Voras Peter Hansen wrote: Ivan Voras wrote: Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a function) sys.exit() is the proper, defined, cross-platform way to exit from a program and return a value to the calling program. Change your definition of elegant and you could consider it easily the most elegant of all solutions. ;-) Ok. :) (Just for the record: I was looking for something that doesn't require a module import. But it is not important.) -- -- Every sufficiently advanced magic is indistinguishable from technology - Arthur C Anticlarke Jul 18 '05 #4 P: n/a Gerrit Holl Ivan Voras wrote: In a code such as: if len(sys.argv)