How to use variables as arguments/parameters to subprocess module
Question:
For example
TestString= 'User'
subprocess.run([r"psshutdown.exe", "\\192.168.0.1 -u "+TestString])
The TestString
is changed to his uppercase USER
.
Why and how can I avoid it?
Answers:
You need to put each command argument in a separate list element. The way you’ve done it, it thinks "\\192.168.0.1 -u "+TestString
is a single parameter, the name of the computer to shut down. Computer names are case-insensitive, so it’s converting it to uppercase.
subprocess.run([r"psshutdown.exe", r"\192.168.0.1", "-u", TestString])
For example
TestString= 'User'
subprocess.run([r"psshutdown.exe", "\\192.168.0.1 -u "+TestString])
The TestString
is changed to his uppercase USER
.
Why and how can I avoid it?
You need to put each command argument in a separate list element. The way you’ve done it, it thinks "\\192.168.0.1 -u "+TestString
is a single parameter, the name of the computer to shut down. Computer names are case-insensitive, so it’s converting it to uppercase.
subprocess.run([r"psshutdown.exe", r"\192.168.0.1", "-u", TestString])