子流程不工作的循环

For loop with sub-process not working

with open('Client.txt','r') as Client_Name: 
   for Client in Client_Name.readlines(): 
       f=file('data_output','w') 
       p1 = subprocess.Popen(['nbcommand', '-byclient', Client], stdout=f, stderr=subprocess.PIPE)
       p1.wait() 
       f.close()

当我将子进程与 for 循环一起使用时。输出为空。但是,如果我只是 #put 一个客户端名称和 运行 脚本,输出就可以了。我如何为不同的客户循环它。有什么建议吗???谢谢,

Client = 'abcd'  
f=file('data_output','w')  
p1 = subprocess.Popen(['nbcommand', '-byclient', Client], stdout=f,stderr=subprocess.PIPE)
p1.wait()  
f.close()

当您使用 for Client in Client_Name.readlines() 时,您会得到最后带有 '\n' 的行。因此,您要么 rstrip() 那个,要么使用 Client_Name.read().splitlines() 来删除换行符,例如:

with open('Client.txt','r') as Client_Name: 
    for Client in Client_Name.read().splitlines(): 
        # rest of code

或:

with open('Client.txt','r') as Client_Name: 
    for Client in Client_Name.readlines(): 
        Client = Client.rstrip()
        # rest of code