我正在尝试在python中运行此命令。
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
我收到以下错误:
BB = os.environ["BB"]
BBDISP = os.environ["BBDISP"]
NOW = datetime.datetime.now()
COLOR="green"
MSG="Widgets loaded successfully."
NODE="server01"
retvalue = os.popen("BB BBDISP \"status NODE.thisIsATest COLOR str(NOW) MSG \"").readlines()
print retvalue
但我能够将该变量打印到屏幕上。
sh: 1: BB: not found
输出
print BB
答案 0 :(得分:1)
您需要从变量中组合字符串。执行此操作的首选方法是使用format
方法:
cmd = "{BB} {BBDISP} \"status {NODE}.thisIsATest {COLOR} str({NOW}) {MSG} \"".format(
BB = os.environ["BB"],
BBDISP = os.environ["BBDISP"],
NOW = datetime.datetime.now(),
COLOR="green",
MSG="Widgets loaded successfully.",
NODE="server01")
答案 1 :(得分:0)
当您调用os.popen时,您传递的字符串必须是操作系统shell中的有效命令。也就是说,如果在终端窗口中键入相同的内容,行为将是相同的。那么问题是:0是' BB'操作系统shell中的有效命令?
我认为您可以通过将字符串更改为popen来实现您想要的目标:
"%s %s \"status %s.thisIsATest %s %s %s \""%(BB,BBDISP,NODE,COLOR,str(NOW),MSG)