Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python print statement prints nothing with a carriage return

I'm trying to write a simple tool that reads files from disc, does some image processing, and returns the result of the algorithm. Since the program can sometimes take awhile, I like to have a progress bar so I know where it is in the program. And since I don't like to clutter up my command line and I'm on a Unix platform, I wanted to use the '\r' character to print the progress bar on only one line.

But when I have this code here, it prints nothing.


# Files is a list with the filenames
for i, f in enumerate(files):
    print '\r%d / %d' % (i, len(files)),
    # Code that takes a long time

I have also tried:


print '\r', i, '/', len(files),

Now just to make sure this worked in python, I tried this:


heartbeat = 1
while True:
    print '\rHello, world', heartbeat,
    heartbeat += 1

This code works perfectly. What's going on? My understanding of carriage returns on Linux was that it would just move the line feed character to the beginning and then I could overwrite old text that was written previously, as long as I don't print a newline anywhere. This doesn't seem to be happening though.

Also, is there a better way to display a progress bar in a command line than what I'm current trying to do?

like image 944
Jonathan Sternberg Avatar asked May 12 '10 18:05

Jonathan Sternberg


People also ask

How do I print a carriage return in Python?

In Python 3, "print" is a function rather than a special operator. It takes an argument called "end" that specifies the line ending to use. 'Print ("This is a test", end = "\r\n")' prints the sentence with a carriage return and newline character.

Why I can print but not return in Python?

The print() function writes, i.e., "prints", a string or a number on the console. The return statement does not print out the value it returns when the function is called. It however causes the function to exit or terminate immediately, even if it is not the last statement of the function.

How do you print without carriage return in Python?

In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') print("It is a great day.")

What is the use of \r in Python?

A carriage return is nothing but a simple escape character. \n is also an escape character which creates a new line. Carriage return or \r is a very unique feature of Python. \r will just work as you have shifted your cursor to the beginning of the string or line.


2 Answers

Try adding sys.stdout.flush() after the print statement. It's possible that print isn't flushing the output until it writes a newline, which doesn't happen here.

like image 130
interjay Avatar answered Sep 19 '22 15:09

interjay


Handling of carriage returns in Linux differs greatly between terminal-emulators.

Normally, one would use terminal escape codes that would tell the terminal emulator to move the virtual "carriage" around the screen (think full-screen programs running over BBS lines). The ones I'm aware of are the VT100 escape codes:

\e[A: up
\e[B: down
\e[C: right
\e[D: left
\e[1~: home
\e[4~: end

Where \e is the escape character, \x1b.

Try replacing all \r's with \e[1~

Also see this post

like image 21
amphetamachine Avatar answered Sep 20 '22 15:09

amphetamachine