2009-05-19 3 views
2
 myfile = open("wrsu"+str(i)+'_'+str(j)+'_'+str(TimesToExec)+".txt",'w') 
     sys.stdout = myfile 
     p1 = subprocess.Popen([pathname,"r", "s","u",str(i),str(j),str(runTime)],stdout=subprocess.PIPE) 
     output = p1.communicate()[0] 
     print output, 

난 내 자신의 파일에 EXE의 출력을 재 지정하려면이 옵션을 사용하여 항상파이썬 2에서 캐리지 리턴을 억제하는 방법은 무엇입니까?

각 줄 끝에서 캐리지 리턴, 어떻게 그것을 억제하기 위해?

답변

2

는 여기에 내가 캐리지 리턴을 제거하는 방법은 다음과 같습니다

p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0] 
for line in p.splitlines(): 
if line.strip(): 
    print line 
+0

안녕하세요, 스플릿 라인이 이미 작업을 수행하고 있으므로 'line.strip()'은'\ n '을 제거 할 필요가 없습니다.'one \ ntwo \ nthree '.splitlines() -> [ 'one' 'two', 'three'] ' –

+1

참. 또한 strip()이 공백과 같은 선행 또는 후행 공백 문자도 제거한다고 생각하십시오. 즉, 유스 케이스에 따라 원하는 것인지 그렇지 않을 것인지를 결정할 수 있습니다. –

1
def Popenstrip(self): 
    p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0] 
    return (line for line in p.splitlines() if line.strip()) 
0
print line.rstrip('\r\n') 

잘 할 것입니다.

관련 문제