2014-04-09 3 views
0

여러 컴퓨터에서 한 행에 여러 번 실행 파일 (delprof2.exe)을 실행하는 작은 프로그램을 작성하려고합니다. PC 이름이 들어간 세 개의 목록을 만들었고 기본적으로 해당 목록의 각 컴퓨터에 대해/c : itroom01 스위치와 함께 실행되도록 실행 파일이 필요하지만 컴퓨터 이름 부분을 코딩하는 방법을 모릅니다. 답장 = 1 섹션에서 내가 얼마나 멀리 있는지 알 수 있습니다.서브 프로세스 라인에 mutiple 인수를 어떻게 추가합니까?

참조 코드 :

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

은 어떤 도움이 가장 극명하게 될 것이다!

감사합니다. Chris.

답변

0

문제는 문자열 형식입니다. 기본 서식 지정 방법을 알고 싶으면 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) 
+0

모든 항목이 문자열이있는 경우, 당신은 단지 문자열 (+) 연결할 수

Lovely, 그 맨 위의 작품은 완벽하게 작동합니다. 나는 더 잘 작동 할 것이므로 저 바닥 하나를 통해 좋은 읽을 거리를 갖게 될 것이다! – user3514446

관련 문제