April 2017
Beginner to intermediate
312 pages
7h 23m
English
The sys module (https://docs.python.org/3/library/sys.html) allows interacting with the Python run-time interpreter. One of the functions of the sys module is parsing command-line arguments provided as inputs to the program. Let's write a program that reads and prints the contents of the file that is passed as an argument to the program:
import sysif __name__ == "__main__": with open(sys.argv[1], 'r') as read_file: print(read_file.read())
Try running the preceding example as follows:
python3 sys_example.py read_lines.txt
The preceding example is available for download along with this chapter as sys_example.py. The list of command-line arguments passed while running the program are available as a argv list in the sys module. ...
Read now
Unlock full access