Python Forum
How to receive two passed cmdline parameters and access them inside a Python script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to receive two passed cmdline parameters and access them inside a Python script?
#1
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
Reply
#2
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receive Input on Same Line? johnywhy 8 732 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,283 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,159 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  How can I add an end line character after every value that I receive? GiggsB 11 5,203 Mar-06-2022, 08:51 PM
Last Post: GiggsB
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 5,637 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  How to receive webcam capture on spout? buzzdarkyear 2 2,671 Jan-12-2022, 02:26 PM
Last Post: buzzdarkyear
  detecting a generstor passed to a funtion Skaperen 9 3,634 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Automating to run python script 100 times by changing parameters pmt 1 2,616 Dec-29-2020, 10:31 AM
Last Post: andydoc
  how to modify a variable that is passed as parameter StefanL38 2 2,126 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  Parameters aren't seen inside function Sancho_Pansa 8 2,956 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020