Introduction
Command-line arguments allow users to pass information to a Python script when it is executed. This information can be used to customize the behavior of the script, such as setting options, providing input data, or specifying output locations. In this article, we will discuss what command line arguments are, how to parse them, and how to use them in your Python programs.
What are command-line arguments?
A command-line argument is a value passed to a script or program when it is executed in a command-line interface (CLI) or terminal. Command-line arguments are used to provide additional information or configuration to a program beyond what is specified in the code itself.
In Python, command-line arguments are accessed through the sys module, which provides access to system-specific parameters and functions. The sys.argv variable contains a list of command-line arguments, with the first argument being the name of the script itself.
For example, if you have a Python program called 'myprogram.py' and you want to pass two arguments to it, you would run the following command in the terminal:
python myprogram.py arg1 arg2
In this example, arg1 and arg2 are the command line arguments that will be passed to 'myprogram.py'.
How to Parse Command Line Arguments
To access the command line arguments in your Python program, you can use the sys module. This module provides access to some variables that contain information about the Python interpreter and the environment it is running in, including the command line arguments.
To parse the command line arguments, you can use the argv variable in the sys module. The argv variable is a list that contains all the command line arguments passed to the program.
Here is an example of how to parse the command line arguments in Python:
import sys
# Get the command line arguments
args = sys.argv
# Print the arguments
print("Command line arguments:", args)
When you run this program with some arguments like python "myprogram.py arg1 arg2", it will output the following:
Command line arguments: ['command-line-arguments.py', 'arg1', 'arg2']
As you can see, the argv variable is a list that contains the name of the program (myprogram.py) and the command line arguments (arg1 and arg2).
How to use Command-Line Arguments
To demonstrate the use of command-line arguments in Python, let's create a simple script that accepts a file name as an argument and prints its contents to the console.
import sys
if len(sys.argv) != 2:
print("Usage: python script.py [filename]")
sys.exit(1)
filename = sys.argv[1]
with open(filename, "r") as f:
for line in f:
print(line.strip())
In this example, we first check that the number of arguments passed to the script is exactly 2 (the script name and the filename). If not, we print a usage message and exit with an error code.
Next, we assign the second argument (the filename) to the filename variable. We then open the file using open() and loop over its lines, printing each line to the console after removing any white-space characters using the strip() method.
To run this script, save it as 'script.py' and execute it in the terminal with the following command:
python script.py input.txt
Assuming there is a file called 'input.txt' in the same directory as the script, the contents of the file will be printed to the console.
Passing Multiple Command-Line Arguments
It is also possible to pass multiple command-line arguments to a Python script. In this case, the sys.argv list will contain all the arguments in the order they were passed.
Here's an example of a script that accepts two arguments, an input file, and an output file, and copies the contents of the input file to the output file:
import sys
if len(sys.argv) != 3:
print("Usage: python script.py [input_file] [output_file]")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
with open(input_file, "r") as fin, open(output_file, "w") as fout:
for line in fin:
fout.write(line)
In this example, we first check that the number of arguments passed to the script is exactly 3 (the script name, the input filename, and the output filename). If not, we print a usage message and exit with an error code.
Next, we assign the input and output filenames to the "input_file" and "output_file" variables, respectively. We then open the input file for reading and the output file for writing using open() with the appropriate modes.
Finally, we loop over the lines in the input file, writing each line to the output file using the write() method.
To run this script, save it as 'copy.py' and execute it in the terminal with the following command:
python copy.py input.txt output.txt
Assuming there is a file called 'input.txt' in the same directory as the script.
Summary
Command line arguments are a powerful feature in Python that allows you to pass information to your programs at runtime through the command line interface. In this article, we have discussed what command line arguments are, and how to parse and use them.
For any queries related to this topic, leave your comment below. You'll get an immediate response.