Python Forum

Full Version: How to pass a variable to subprocess.check_call
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'])
subprocess.check_call([ r'{}\ccleaner.exe'.format(mypath), '/auto' ])