This question already has answers here:
Exit codes in Python
(14 answers)
Closed 4 years ago.
In C, I'd do something like
int main() {
return 42;
}
and after executing the program, I can(in Linux) prompt echo $? to the console to get the desired 42 as the return signal from the last operation. How can I do that in python?
I tried just creating a file with a return 42 in it, but it says that it is not inside a function(obviously).
exit(42)
will give you the desired behaviour.
Related
This question already has an answer here:
Different behavior in Python script and Python IDLE?
(1 answer)
Closed 9 months ago.
a = 1000
b = 1000
a is b
So if you run this code using pycharm or command python filename.py a is b = True
If you run this code using command line a is b = Fasle.
I know about implemented array in python (-5 .. 256), but the question is why it's True when you run your code not from command line?
Normally, a is b checks if both variables refer to the same object. And a == b checks for equality of their values. However, that might not be the case in python for small integers. Look at this reddit post with similar question to get more info.
This question already has answers here:
drop into python interpreter while executing function
(3 answers)
Closed 5 years ago.
Is there a way to cause a python script to enter the interpreter at a given line? I know I can use import pdb; pdb.set_trace() to begin debugging at a certain line, but that means all the pdb commands may get in the way.
Is there a way to kick into the regular interpreter (or ipython) outside of pdb?
The easiest way is code.interact:
import code
code.interact(local={**globals(), **locals()})
This question already has answers here:
Manually raising (throwing) an exception in Python
(11 answers)
Closed 6 years ago.
I want to create an error message in Python, i.e. the program should be interrupted and an (if possible coloured) error message should be printed. For instance:
If a == 0:
Error("a should be nonzero")
In Matlab, you can do this using the error instruction. How can you do this in Python? I have found this page but I am not sure that is what I am looking for.
You can raise it like so:
if a == 0:
raise ValueError("a should be nonzero")
or simply by using assert as:
assert a!=0, "a should be nonzero!"
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 7 years ago.
Im trying to make a python program, and i just can't find any information on Google on how to just execute a non-python file from python (with an if statement). For Example:
if abc == "abc":
"Execute ThisFile.sh"
EDIT: this was marked as dupe, but I am not trying to run a single command, I want to execute the entire file.
Use subprocess module.
if abc == 'abc':
subprocess.check_output('bash file.sh', shell=True)
This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 8 years ago.
So i've been at this one for a little while and cant seem to get it. Im trying to execute a python script via terminal and want to pass a string value with it. That way, when the script starts, it can check that value and act accordingly. Like this:
sudo python myscript.py mystring
How can i go about doing this. I know there's a way to start and stop a script using bash, but thats not really what im looking for. Any and all help accepted!
Try the following inside ur script:
import sys
arg1 = str(sys.argv[1])
print(arg1)
Since you are passing a string, you need to pass it in quotes:
sudo python myscript.py 'mystring'
Also, you shouldn't have to run it with sudo.