MCS 260 Fall 2020
Emily Dumas
Taking a break from Python, let's talk about the shell.
When you run a command in the shell, it may accept some strings as arguments, e.g.
PS C:\Users\ddumas> cd Desktop
PS C:\Users\ddumas\Desktop>
Here $\texttt{cd}$ is the command name, and the string $\texttt{Desktop}$ is the first (and only) command line argument.
Command line arguments are separated by spaces.
Python programs can access the command line arguments. For example, if a script is run with the command
python example.py now is the winter of our discontent
Then we can access each string after "python". This is useful so that a program can accept input from the command line, rather than reading it from the keyboard.
To access command line args, we first import the $\texttt{sys}$ module:
import sys
Now we have access to the list $\texttt{sys.argv}$. At index $0$ it contains the name of our script (as given to the interpreter). At index $1$ is the first argument after the script name, etc..
In the previous example, $\texttt{sys.argv}$ would have value:
['example.py', 'now', 'is', 'the',
'winter', 'of', 'our', 'discontent']
Simple example: repeat0.py, repeats a string a given number of times
"""Repeat a string a given number of times.
The first argument is the number of times.
The second gives the string to repeat.
"""
import sys
n = int(sys.argv[1])
s = sys.argv[2]
for i in range(n):
print(s)
PS C:\Users\ddumas\Desktop> python repeat0.py 5 hello
hello
hello
hello
hello
hello
PS C:\Users\ddumas\Desktop> python repeat0.py onlyone
Traceback (most recent call last):
File "repeat0.py", line 7, in <module>
n = int(sys.argv[1])
ValueError: invalid literal for int() with base 10: 'onlyone'
PS C:\Users\ddumas\Desktop> python repeat0.py
Traceback (most recent call last):
File "repeat0.py", line 7, in <module>
n = int(sys.argv[1])
IndexError: list index out of range
PS C:\Users\ddumas\Desktop>
The better version repeat.py checks for too few arguments and handles it gracefully.
"""Repeat a string a given number of times.
The first argument is the number of times.
The second gives the string to repeat.
"""
import sys
if len(sys.argv) < 3:
print("Usage:",sys.argv[0],"N s")
print("Prints N copies of string s, one per line.")
else:
n = int(sys.argv[1])
s = sys.argv[2]
for i in range(n):
print(s)
PS C:\Users\ddumas\Desktop> python repeat.py
Usage: repeat.py N s
Prints N copies of string s, one per line.
PS C:\Users\ddumas\Desktop> python repeat.py 3
Usage: repeat.py N s
Prints N copies of string s, one per line.
PS C:\Users\ddumas\Desktop> python repeat.py 3 goodbye
goodbye
goodbye
goodbye
The handoff of arguments from the shell to the Python script is one of the services of the operating system or OS.
Windows, Linux, Mac OS, Android, iOS are all operating systems.
An OS manages the lowest-level details of a computer's operation.
A key feature of operating systems is that they provide abstraction.
For example: A wireless mouse, a wired mouse, and a touchpad operate very differently. The OS handles these differences so that a program can ask for the current position of the pointer, without concern for the specific hardware.
At a low level (hardware), assuming a wireless mouse:
At the level of OS-provided functions: