...

/

Command-line Arguments

Command-line Arguments

Learn about the usage of arguments passed to a Python script.

Arguments passed to a Python script are available in sys.argv.

Press + to interact
# main.py
import sys
print('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 ...