Batch 7 Python
Batch 7 Python
Batch 7
K.SHIVANI (242FA05029)
G.MOHITH VENU REDDY (242FA05024)
MANOJ BHUDANIYA (242FA05042)
PANDAY THUFAN (242FA05051)
Understanding sys.argv
What is sys.argv? Argument Indexing
A list holding command-line arguments passed sys.argv[0] is the script name. Subsequent indexes
to a Python script. hold inputs.
Example Usage :
import sys
import sys
try:
TO RUN IN TERMINAL
if len(sys.argv) < 3: python add_args.py 10 20
raise ValueError("Two numbers must be provided.")
•sys.path is a list of strings that specifies the search path for Python modules.
Modifying sys.path:
You can manually add custom directories to sys.path to import modules from non-standard locations .
import sys
sys.path.append('/home/user/my_modules') # Add custom path
❌ Cons:
def show_help():
print("Usage: python script.py --sum|--avg|--max num1 num2 ...")
print("Example: python script.py --avg 10 20 30") EXAMPLE RUN :
python script.py --max 3 15 9
try:
if len(sys.argv) < 3:
raise ValueError("Not enough arguments.")
flag = sys.argv[1]
numbers = list(map(float, sys.argv[2:]))
1) 3)
python script.py --sum abc python script.py --help
OUTPUT:
Error: could not convert string to float: 'abc' OUTPUT:
Usage: python script.py --sum|--avg|--max num1 num2 ...
2)
python script.py
OUTPUT
Error: Not enough arguments.