Python Command-Line Arguments
Python is a versatile programming language that allows developers to create powerful applications and scripts. One of the features that make Python so flexible is its ability to accept command-line arguments. Command-line arguments are values or options that are passed to a program when it is executed from the command line. These arguments can be used to modify the behavior of the program or provide input data.
How to Use Command-Line Arguments in Python
In Python, command-line arguments are accessed through the sys.argv list. This list contains the command-line arguments passed to the script. The first element of the list, sys.argv[0], is always the name of the script itself.
Let’s take a look at an example to understand how command-line arguments work in Python:
import sys
# Print the number of command-line arguments
print("Number of arguments:", len(sys.argv))
# Print the command-line arguments
for arg in sys.argv:
print(arg)
When you run this script from the command line with some arguments, it will display the number of arguments and then print each argument on a new line. For example, if you run the script with the command python script.py arg1 arg2 arg3, the output will be:
Number of arguments: 4 script.py arg1 arg2 arg3
Using Command-Line Arguments in Your Python Scripts
Command-line arguments can be used in various ways to enhance the functionality and flexibility of your Python scripts. Here are a few examples:
1. Providing Input Data
You can use command-line arguments to provide input data to your script. For example, if you have a script that performs some calculations on a given number, you can pass that number as a command-line argument. Here’s an example:
import sys
# Get the number from the command-line argument
number = int(sys.argv[1])
# Perform some calculations
result = number * 2
# Print the result
print("Result:", result)
If you run this script with the command python script.py 5, it will multiply the number 5 by 2 and print the result:
Result: 10
2. Modifying Script Behavior
Command-line arguments can also be used to modify the behavior of your script. For example, you can use a command-line argument to enable or disable certain features or set specific options. Here’s an example:
import sys
# Check if the debug flag is provided
if "--debug" in sys.argv:
# Enable debug mode
debug = True
else:
# Disable debug mode
debug = False
# Print debug information if debug mode is enabled
if debug:
print("Debug mode enabled")
else:
print("Debug mode disabled")
If you run this script with the command python script.py --debug, it will enable the debug mode and print “Debug mode enabled”. If you run it without the --debug flag, it will disable the debug mode and print “Debug mode disabled”.
3. Processing Multiple Files
Command-line arguments can also be used to process multiple files or perform batch operations. For example, you can write a script that takes a list of file names as command-line arguments and performs the same operation on each file. Here’s an example:
import sys
# Process each file
for filename in sys.argv[1:]:
# Open the file and perform some operation
with open(filename, "r") as file:
# Do something with the file
print("Processing:", filename)
If you run this script with the command python script.py file1.txt file2.txt file3.txt, it will process each file and print “Processing: file1.txt”, “Processing: file2.txt”, and “Processing: file3.txt”.
Conclusion
Python command-line arguments provide a convenient way to modify the behavior of your scripts and provide input data. By using the sys.argv list, you can access the command-line arguments passed to your script and use them to enhance its functionality. Whether you need to provide input data, modify script behavior, or process multiple files, command-line arguments can help you achieve your goals.
