How do I access com...
 
Notifications
Clear all

How do I access command line arguments in Python?

   RSS

1
Topic starter

I'm trying to write a Python script that can accept command-line arguments. Can someone provide a simple example of how to access command-line arguments in a Python script, and explain how it works?

1 Answer
0

A quick way to access command-line arguments in Python is by using the sys module. Here’s a basic example:

import sys
print("Script Name:", sys.argv[0])
print("First Argument:", sys.argv[1])

Just remember that sys.argv is a list, where the first item (sys.argv[0]) is always the script's name.

Share: