Python Forum
How to receive two passed cmdline parameters and access them inside a Python script? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to receive two passed cmdline parameters and access them inside a Python script? (/thread-41610.html)



How to receive two passed cmdline parameters and access them inside a Python script? - pstein - Feb-17-2024

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


RE: How to receive two passed cmdline parameters and access them inside a Python script? - Gribouillis - Feb-17-2024

(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.


RE: How to receive two passed cmdline parameters and access them inside a Python script? - deanhystad - Feb-17-2024

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.