Command-line Arguments
Learn about the usage of arguments passed to a Python script.
We'll cover the following...
Arguments passed to a Python script are available in sys.argv
.
Press + to interact
# main.pyimport sysprint('Number of arguments received = ', len(sys.argv))print('Arguments received = ', str(sys.argv))
If we want to write a program for copying the contents of one file to another, we can receive source and target filenames through command-line arguments.
Copying one file's contents to another using command-line arguments
Parsing the command line
When using the filecopy.py
program discussed above, the first filename is always treated as a source, and the second as the target. Instead of this, if we want to have flexibility in supplying source and target filenames, we can use options ...