如何向子流程行添加多个参数?

2024-06-16 13:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个小程序,在多台计算机上连续多次运行一个可执行文件(delprof2.exe)。我已经用中的PC名称创建了三个列表,并且基本上需要使用开关/c:itroom01(例如)为列表中的每台机器运行可执行文件,但是我不知道如何对机器名称部分进行编码-您可以从reply=1部分看到我已经做了多少。你知道吗

参见代码:

import os
import subprocess

itroom = ["itroom01", "itroom02", "itroom03"]
second = ["2nditroom01", "2nditroom02", "2nditroom03"]
csupport = ["csupport-m30", "csupport-m31", "csupport-m32"]

print "Which room's PCs do you want to clear out?"
print "\t(1) = ITRoom"
print "\t(2) = 2nd ITRoom"
print "\t(3) = Curriculum Support"
reply = input("Enter 1, 2 or 3: ")

if reply == 1:
    for item in itroom:
        subprocess.call(['c:\delprof2\DelProf2.exe /l /c:'%itroom])
        raw_input("Press return to continue...")
elif reply == 2:
    for item in second:
        subprocess.call("c:\delprof2\DelProf2.exe /l")
        raw_input("Press return to continue...")
elif reply == 3:
    for item in csupport:
        subprocess.call("c:\delprof2\DelProf2.exe /l")
        raw_input("Press return to continue...")
else: 
    print "invalid response"
    raw_input("Press return to continue...")

任何帮助都将不胜感激!你知道吗

谢谢你, 克里斯。你知道吗


Tags: toforinputrawreturnitemreplyexe
1条回答
网友
1楼 · 发布于 2024-06-16 13:31:03

你的问题是字符串格式。阅读the tutorial了解如何进行基本格式化。你知道吗

如果所有项都是字符串,则可以将字符串连接起来(+):

import subprocess

reply = int(raw_input("Enter 1, 2 or 3: "))
for item in [itroom, second, csupport][reply - 1]:
    subprocess.check_call([r'c:\delprof2\DelProf2.exe', '/l', '/c:' + item])

注意:如果希望所有子进程同时运行,则可以直接使用Popen

import subprocess

reply = int(raw_input("Enter 1, 2 or 3: "))
commands = [[r'c:\delprof2\DelProf2.exe', '/l', '/c:' + item]
            for item in [itroom, second, csupport][reply - 1]]
# start child processes in parallel
children = map(subprocess.Popen, commands) 
# wait for processes to complete, raise an exception if any of subprocesses fail
for process, cmd in zip(children, commands):
    if process.wait() != 0: # failed
       raise subprocess.CalledProcessError(process.returncode, cmd)

相关问题 更多 >