7.1. System variables
- os.getcwd() - The current working directory
- os.chdir(newpath) - Changes current working directory to newpath
- sys.argv - Program name, and command-line arguments
- sys.path - Python path (list of folders that Python looks in for functions and modules)
- sys.version - Version of Python running
7.2. Program arguments
One of the more important variables in the sys module is sys.argv, which is a list containing all the arguments passed to the program at run time. This is sort of the standard way of interacting with command-line programs in Unix: you type the name of the program, followed by any parameters, at the command prompt:
python code
[example unix prompt]# python pythontest.py Arg1 Arg2
After importing the sys module, you may access the list sys.argv, which is a list of strings, the first of which is the name of the program running, and the rest of which are the command-line arguments:
python code
print(sys.argv) # "[pythontest.py Arg1 Arg2]"
For complex argument processing, you should probably use the Getopt or argparse modules.
Up to Index
Previous: 6. Functions & Includes | Next: 8. Debugging