Skip to content Skip to sidebar Skip to footer

Subprocess In Python Add Variables

Subprocess in Python Add Variables import subprocess subprocess.call('Schtasks /create /sc ONCE /tn Work /tr C:\work.exe /st 15:42 /sd 13/10/2010') I want to be able to set th

Solution 1:

Use % formatting to build the command string.

>>>hour,minute='15','42'>>>day,month,year='13','10','2010'>>> command ='Schtasks /create /sc  ONCE  /tn  Work  /tr C:\work.exe /st %s:%s /sd %s/%s/%s'>>> command % (hour,minute, day,month,year)
'Schtasks /create /sc  ONCE  /tn  Work  /tr C:\\work.exe /st 15:42 /sd 13/10/2010'>>> subprocess.call( command % (hour,minute, day,month,year) )
>>>

Solution 2:


import subprocess 
time= "15:42"
date= "13/10/2010"
# you can use these variables anyhow take input fromuserusing raw_iput()
subprocess.call('Schtasks /create /sc ONCE /tn Work /tr C:\work.exe /st '+time+' /sd '+date)

Solution 3:

Python has advanced string formatting capabilities, using the format method on strings. For instance:

>>>template = "Hello, {name}. How are you today, {date}?">>>name = "World">>>date = "the fourteenth of October">>>template.format(name=name, date=date)
'Hello, World. How are you today, the fourteenth of October?'

You can get the time and date using strftime in the datetime module:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%A %B %Y, %I:%M:%S")
'Wednesday October 2010, 02:54:30'

Solution 4:

import time

subprocess.call(time.strftime("Schtasks /create /sc  ONCE  /tn  Work  /tr C:\work.exe /st %H:%M /sd %d/%m/%Y"))

If you would like to change the time you can set it into time object and use it.

Post a Comment for "Subprocess In Python Add Variables"