Search code examples
pythonbashpython-2.7carriage-return

python 2.7 | carriage return not working


I am trying to use \r to print to the same line in bash (Ubuntu 16.04 terminal) but it doesn't work, as the program still prints frame number in a new line.

Here is the code

i = 0
while img is not None:
    print "Frame Number: {0}  \r".format(i)
    result = unwarp(img, xmap, ymap)
    result.save(disp)
    # Save to file
    fname = "../temp_data/frames/FY{num:06d}.png".format(num=i)
    result.save(fname)        
    img = vc.getImage()
    i = i + 1

I even tried using \x08 like this

print "Frame Number: {0}  \xO8".format(i)

but its still not working.

here is a sample output:

Frame Number: 0  
Frame Number: 1  
Frame Number: 2  
Frame Number: 3  
Frame Number: 4  
Frame Number: 5  
Frame Number: 6  
Frame Number: 7  
Frame Number: 8

Solution

  • Try it like this:

    print "\rFrame Number: {:06d}".format(i),
    

    Note the trailing , character on the print statement.