Python Forum
How to pass a variable to subprocess.check_call - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to pass a variable to subprocess.check_call (/thread-16792.html)



How to pass a variable to subprocess.check_call - champweller - Mar-15-2019

Hey Fellow forum users!

I was hoping someone could help me understand how to simplify my code.

I am trying to write a simple script that runs a bat file and some applications to automate the cleaning of a PC. My script is working fine ,however, I would like to improve my code so that I can change the path to the applications via variable rather than changing each line that holds the path to the application.

I would like to use something like this

mypath = "\\localhost\SOFTWARE"
However I can't find a way to do this after all my google searches I thought something like the below might work however after this change my script would not run. (I can get the error if needed)

subprocess.check_call([r'{}\ccleaner.exe','/auto'.format(mypath)])
Below is my working script. Could anyone help and give me some insight on what I am trying to accomplish?

import os
import subprocess


os.chdir(r'\\localhost\\SOFTWARE')
os.startfile('win7tempclean.bat')
subprocess.check_call([r'\\localhost\SOFTWARE\ccleaner.exe','/auto'])
subprocess.check_call([r'\\localhost\SOFTWARE\hmp.exe','/scan','/quiet'])



RE: How to pass a variable to subprocess.check_call - heiner55 - May-26-2019

subprocess.check_call([ r'{}\ccleaner.exe'.format(mypath), '/auto' ])