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’,有什么想法吗?
提前感谢
乔治
发布于 2010-10-13 21:50:00
使用% 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) )
>>> 发布于 2010-10-13 21:28:36
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)发布于 2010-10-13 21:50:23
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'https://stackoverflow.com/questions/3924122
复制相似问题