F
F
fire_engel2020-05-11 18:10:55
Python
fire_engel, 2020-05-11 18:10:55

How to pass arguments to a python file from the command line?

Hello, I have the following bat file
python C:\test.pycalled load.
I need to make it so that I can pass arguments to the file directly from the command line, for example:
if the file is test.py:
print(d,c,b,a)
then load 1 2 3 4 on the command line should output 4 3 2 1. The question is how to get these a ,b,c,d.
edit:
Tried to solve with sys.argv method, but it only returns the path to the file.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2020-05-11
@res2001

For the simple version:

import sys
print(sys.argv[1])

sys.argv - list with command line parameters
For more advanced parameter parsing, use the argparse module.

S
shading, 2020-05-11
@shading

https://jenyay.net/Programming/Argparse I
recommend reading the article

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question