We want to create programs that can be launched (executed) from the Terminal command line.
We may have covered the following example:
Create a program that prints the date and time for the user.
Let's create a program in a file called Now.py. We'd like the following interaction in the Terminal (what you type is in bold):
$ ./Now
The time now is Mon May 6 21:48:10 2019
First, we'll create the file Now.py two slightly different ways. Here's the contents of the file the first way:
#! /usr/bin/python3
import time
def main():
print('The time now is %s' % time.asctime())
main()
Here's the second way:
#! /usr/bin/python3
import time
print('The time now is %s' % time.asctime())
What's up with the "#! /usr/bin/python3" line at the top of the file?
The "#! /usr/bin/python3" must be the first line in your file so that the Linux/Unix operating system will know that the file contains Python3 commands, and so that the operating system will bring in the Python3 interpreter, which is located in the /usr/bin directory to interpret and execute the Python3 commands inside the Now.py file. When executing python program on a Windows computer, this line is not needed. However, we are not only using Unix on the classroom computers, but any "back-end" Python program used for website interaction will be executed on a Unix computer, and therefore this line is necessary.
We must also make sure that the program file, Now.py, has "execute" permissions.
We do this by changing the default persmissions of the file (which are "-rw-r--r--") to "-rwxr-xr-x". The "x" in the permissions string indicates that this file contains program commands to be executed and not just data. Here's how we add execute permissions to our file:
$ chmod +x Now.py
Suppose we wanted our program to be given arguments (words) on the Unix command-line -- words that the program can use?
For instance, let's write a program that upper-cases all the words on the command line when it is called. The program file will be called upper.py. We want to be able to do this:
$ ./upper.py hi there, Whatever YOU say.
./UPPER.PY HI THERE, WHATEVER YOU SAY.
All the words on the command-line can be brought into the program using the Python sys library. The sys library has a item called sys.argv which is a list of strings -- it is the list of words on the command line, starting with the first word being the name of the program you typed. For instance, in the example above, sys.argv = ['./upper.py', 'hi', 'there,', 'Whatever', 'YOU', 'say.']. So, here's a version of the upper.py program:
#! /usr/bin/python3
import sys
s = ""
for aword in sys.argv:
s += aword.upper() + " "
print(s[:-1])
There's a traditional way of designing programs that will be executed from the command-line...
Notice that in the example above, there is no function defined. Just a sequence of commands in the left-hand margin. That's fine. In other languages, however, it's traditional (well, actually required) to create a function called main() that will be automatically executed as the first step in the execution of the program. This requirement applies to languages like Java, C and C++. Python doesn't have such a requirement. But programmers coming from other languages tend to design their programs with such a pattern in mind.
So the program above can be redesigned like so:
#! /usr/bin/python3
import sys
def main(lst):
for aword in lst:
s += aword.upper() + " "
print(s[:-1])
main(sys.argv)
Notice that once main() is defined, it still has to be executed.