0
import subprocess
result = subprocess.call('"C:\\Users\\user\\Desktop\\Install Exe\\download.exe" /S norestart ', shell=True)
print(result)

This is something hardcode in the command, I want to pass this path in the variable and want variable instead of the path

Can we do this thing?

script0
  • 387
  • 2
  • 12

1 Answers1

0

Just use

subprocess.run(["arg1", "arg2", "arg3"])

so say I wanted to move a file from "/home/myfile.txt" to "/home/data" which accepts the arguments sourcefile and destination folder I would do

subprocess.run(["mv", "/home/myfile.txt", "/home/data"])

any of these strings can be variables and the strings can be f-string formatted also

rab1262
  • 51
  • 5
  • import subprocess source_url="https://github.com/notepad-plus-plus/notepad-plus-plus/releases/download/v8.1.9.2/npp.8.1.9.2.Installer.exe" destination=f'C:\\Users\\user\\Desktop\\Install Exe\\download.exe' download =urlretrieve(source_url,destination) result = subprocess.call('C:\\Users\\user\\Desktop\\Install Exe\\download.exe /S norestart ', shell=True) print(result) how to write a variable name in subprocess command instead of hard code path – Rishi Vashishtha Dec 16 '21 at 14:35
  • @RishiVashishtha, this answer tells you to stop using `shell=True` and instead pass a list of individual arguments. You can use a variable for any of those arguments. – Charles Duffy Dec 16 '21 at 17:47
  • @RishiVashishtha if this answers you're question would you mind accepting it please – rab1262 Dec 17 '21 at 16:06