CLI Program Execution
Table of contents
Description
You can easily execute your Python programs directly from the command line. First, navigate to the directory the .py
file is stored. Then, use the python3
command to execute the file.
addition.py
The following program prompts the user for two integers using the input()
function, then displays the sum. It is saved in the /Users/bianca/test
directory.
def main():
try:
number1 = int(input('Enter 1st number: '))
number2 = int(input('Enter 2nd number: '))
except ValueError:
print('\nYou must enter two integer arguments. Ending Execution')
return
print(f'\nThe sum is {number1 + number2}')
if __name__ == '__main__':
main()
Navigate to directory and execute:
addition-cli.py
The following program accepts two integer arguments from the command line, then displays the sum. It is saved in the /Users/bianca/test
directory.
import sys
def main():
try:
number1 = int(sys.argv[1])
number2 = int(sys.argv[2])
except ValueError:
print('\nYou must enter two integer arguments. Ending Execution')
return
print(f'\nThe sum is {number1 + number2}')
if __name__ == '__main__':
main()