Python Return Error Codes
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 python script return value to shell Us Learn more about Stack Overflow the company Business Learn more about hiring
Python Exit Code 2
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the python sys.exit example Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Best practice in python for return python get exit code 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 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
Python Exception Exit Code
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.5k135688 asked Oct 27 '09 at 13:11 TallPaul 2531612 1 Well as I see it: The intersection with an empty list results just in an empty list and not an exception. –stefanw Oct 27 '09 at 13:21 Well, I DID say "in g
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 python exit with error message Us Learn more about Stack Overflow the company Business Learn more about hiring python exit code 255 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Python Check Exit Code
Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Best way to return a http://stackoverflow.com/questions/1630706/best-practice-in-python-for-return-value-on-error-vs-success value from a python script up vote 19 down vote favorite 12 I wrote a script 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 http://stackoverflow.com/questions/18231415/best-way-to-return-a-value-from-a-python-script 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 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
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get https://docs.python.org/2.7/tutorial/errors.html while you are still learning Python: >>> while True print 'Hello world' File "
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