Reputation: 3
I'm pretty new to python and linux and I'm running into a problem...
So I'm trying to run the following code (It is supposed to inform the person in front of the terminal, that the program is still running and then delete that line after 1 second):
import sys
while True:
# do something
sys.stdout.write("Still going...")
time.sleep(1)
sys.stdout.write("\r")
sys.stdout.flush()
This works perfectly fine on windows on python 3.8, but when i run it on my linux vps with python 3.6.9 via the "python3" command it doesn't flush the "\r", so the "Still going..." line only gets deleted and immediately reprinted the next time it reaches sys.stdout.write("Still going...").
If anyone has an idea what's going on here - please tell me Any help is appreciated!
Upvotes: 0
Views: 107
Reputation: 6209
Windows, Mac OS and UNIXes code new lines with differents chars.
\r\n
\r
\n
if you want your program to be cross-platform, you should use os.linesep
instead of an OS-specific linebreak
answering the comment:
Indeed on Windows, \r
just return at the start of the line while \n
actually starts a new line (see this StackExchange anwser for a nice explanation).
I assume that, on windows, it allows you to simply write on the same line until the program exits.
Sadly it could work at least with some terminals on UNIXes but not necessarily on every terminals...
As a work around, you could probably use the \b
character which deletes the last caracter of the line, like the [backspace]
key.
Upvotes: 1