Python sys Module

The main use I’ve found for the Python sys module is allowing command-line arguments to be made to a script. Here is an example of how it looks:


import sys

if len(sys.argv) == 2:
    input_file = sys.argv[1]
else:
    print "Please input a command-line argument specifying the file"

This script checks that 2 command-line arguments had been passed to the program before assigning the value sys.argv[1] to a variable. We check for two command-line arguments because the first one (sys.argv[0]) is the name of the Python script currently being executed.

The following two tabs change content below.
Computational biology PhD candidate at the Australian National University. I love writing (both articles and software), learning more about the world around us, and beekeeping. I also write for BioSky.co

Latest posts by Jack Simpson (see all)

Comments are closed.