Koloktos Koloktos - 3 years ago 79
Python Question

Calling batch file that calls python script that uses parameter defined in initial batch file

I've two batch files, A.bat and B.bat, as well as a python script C.py

A.bat looks something like this:

call B.bat hello

Now of course, if I use %1 in B.bat, the batch file will view that value as "hello". Now, if I have B.bat call C.py, I need C.py to be able to recognize that %1 is hello, the same way B.bat recognizes that.

Of course, I've tried simply writing in B.bat:

call C.py %1

And then using %1 in C.py like I would in any batch file, but this did not work. I don't know if this means that I can't use %1 itself as a parameter or if I can't use a parameter of this type in python, or both. So, how would I be able to use a parameter in C.py that was defined in A.bat?

Answer Source

To access parameters in Python, use:

import sys
print(sys.argv[1])  # view the first parameter, similar to %1 in .bat

Ref: https://docs.python.org/3/library/sys.html#sys.argv

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download