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?

Asked By: Squalo

||

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])
Answered By: Barmar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.