Python中的子流程添加变量
import subprocess
subprocess.call('Schtasks /create /sc ONCE /tn Work /tr C:\work.exe /st 15:42 /sd 13/10/2010')
我希望能够在上面的命令中设置变量。变量是时间'15:42'在15和42之间分开,日期'13 / 10/2010'在日,月和年中分开任何想法?
提前完成
乔治
答案 0 :(得分:1)
使用%
formatting构建命令字符串。
>>> 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) )
>>>
答案 1 :(得分:0)
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)
答案 2 :(得分:0)
Python在字符串上使用format
方法具有高级字符串格式化功能。例如:
>>> 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?'
您可以使用datetime
模块中的strftime
获取时间和日期:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%A %B %Y, %I:%M:%S")
'Wednesday October 2010, 02:54:30'
答案 3 :(得分:0)
import time
subprocess.call(time.strftime("Schtasks /create /sc ONCE /tn Work /tr C:\work.exe /st %H:%M /sd %d/%m/%Y"))
如果您想更改时间,可以将其设置为时间对象并使用它。