Python Forum

Full Version: How to receive two passed cmdline parameters and access them inside a Python script?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Sorry for this newbie question:

Assume I have have a python script myscript.py (on Windows 10).

Now I want to call this script (from command line) and pass two parameters (separrted by blanks) e.g.

myscript.py "D:\tools\input data.txt" 65656

The parameters may or may not enclosed in double quotes (as above).

How can I access these passed parameters from inside the script?
Say I want to echo/output them (=the values) simply on command line.

It would be nice to get a sample script.

Thank you
Peter
(Feb-17-2024, 08:55 AM)pstein Wrote: [ -> ]How can I access these passed parameters from inside the script?
Say I want to echo/output them (=the values) simply on command line.

It would be nice to get a sample script.
Use the standard library module argparse.

Some examples are given in this blog post.
For something this simple you can use sys.argv
import sys

print(sys.argv)
Output:
> python test.py "D:\tools\input data.txt" 65656 ['test.py', 'D:\\tools\\input data.txt', '65656']
Notice that 65656 is a str, not a number.