Python Forum
How to pass a variable to subprocess.check_call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass a variable to subprocess.check_call
#1
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'])
Reply
#2
subprocess.check_call([ r'{}\ccleaner.exe'.format(mypath), '/auto' ])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 807 Jul-27-2023, 12:40 AM
Last Post: tester_V
  Pass variable to subprocess paulo79 4 9,959 Apr-12-2022, 12:35 PM
Last Post: DeaD_EyE
  how to pass the interactive string to Popen subprocess maiya 1 1,846 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,514 Sep-07-2020, 08:02 AM
Last Post: perfringo
  How to pass multiple values from one sample to nc variable? Baloch 0 1,838 Jun-01-2020, 09:27 PM
Last Post: Baloch
  How to pass variable as an argument to be used in a function? t4keheart 8 3,522 Jan-16-2020, 01:37 PM
Last Post: buran
  Pass by reference vs Pass by value leodavinci1990 1 2,165 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  Pass variable script return twice output problem Faruk 8 4,295 Dec-26-2018, 11:57 AM
Last Post: Faruk
  how to pass a variable from a file to a 2d file sylas 1 2,181 May-05-2018, 02:46 AM
Last Post: micseydel
  [subprocess]>Run a cmd command and get output into a variable CSA75 4 22,710 Mar-13-2017, 09:33 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020