geocheats2
geocheats2

Reputation: 31

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 the variables on the above command. the variables are the time '15:42' separated in 15 and 42 and the date '13/10/2010' separated in day , month and year any ideas??

Thanx in advance

George

Upvotes: 1

Views: 1008

Answers (4)

Version Control Buddy
Version Control Buddy

Reputation: 1446

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.

Upvotes: 0

Katriel
Katriel

Reputation: 123782

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'

Upvotes: 0

gimel
gimel

Reputation: 86512

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) )
>>> 

Upvotes: 1

jknair
jknair

Reputation: 4774


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

Upvotes: 0

Related Questions