对于子流程不起作用的循环

时间:2017-09-19 09:41:18

标签: python subprocess popen

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()

1 个答案:

答案 0 :(得分:0)

当您使用if ("csv".equals(format)) { response.setContentType("text/csv; charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("\uFEFF"); out.print(buildMonthlyDetailsItemCsv(list)); return null; } 时,最后会得到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