user53670
user53670

Reputation:

How to suppress the carriage return in python 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,

When I use this to redirect the output of a exe to my own file, it always

a carriage return after each line, How to suppress it?

Upvotes: 2

Views: 4003

Answers (3)

xiaolong
xiaolong

Reputation: 3647

print line.rstrip('\r\n') 

will do fine.

Upvotes: 0

odwl
odwl

Reputation: 2123

def Popenstrip(self):
  p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0]
  return (line for line in p.splitlines() if line.strip())

Upvotes: 1

Rob Carr
Rob Carr

Reputation: 270

Here's how I removed the carriage return:

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

Upvotes: 2

Related Questions