Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A How to overwrite lines of STDOUT in Python?

The solution proposed by the other answer works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example: print("this is some text...

posted 1y ago by hkotsubo‭  ·  edited 1y ago by hkotsubo‭

Answer
#4: Post edited by user avatar hkotsubo‭ · 2023-10-09T17:23:54Z (about 1 year ago)
  • The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:
  • ```python
  • print("this is some text", end="\r")
  • print("abc")
  • ```
  • Testing this in a Linux terminal, the output is:
  • ```none
  • abcs is some text
  • ```
  • But I guess it should be only "abc", right? The rest of this answer assumes that that's the case.
  • IMO, the following isn't a pretty solution, but anyway: you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from `stdin`:
  • ```python
  • last_line = ''
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(' ' * len(last_line), end='\r') # spaces to "clear" the line
  • print(line, end='\r')
  • last_line = line
  • print() # just to print a newline after the last line
  • ```
  • Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).
  • ---
  • A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line before printing the next message:
  • ```python
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(f"\033[K{line}", end='\r')
  • print()
  • ```
  • With this solution, the first example will be:
  • ```python
  • print("this is some text", end="\r")
  • print("\033[Kabc")
  • ```
  • In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing the rest, so the output is just `abc`.
  • ---
  • Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):
  • ```python
  • import colorama
  • colorama.just_fix_windows_console()
  • print("this is some text", end="\r")
  • print(f"{colorama.ansi.clear_line()}abc")
  • ```
  • The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:
  • ```python
  • print("this is some text", end="\r")
  • print("abc")
  • ```
  • Testing this in a Linux terminal, the output is:
  • ```none
  • abcs is some text
  • ```
  • But I guess it should be only "abc", right? The rest of this answer assumes that that's the case.
  • IMO, the following isn't a pretty solution, but anyway: you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from `stdin`:
  • ```python
  • last_line = ''
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(' ' * len(last_line), end='\r') # spaces to "clear" the line
  • print(line, end='\r')
  • last_line = line
  • print() # just to print a newline after the last line
  • ```
  • Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).
  • ---
  • A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line before printing the next message:
  • ```python
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(f"\033[K{line}", end='\r')
  • print()
  • ```
  • With this solution, the first example will be:
  • ```python
  • print("this is some text", end="\r")
  • print("\033[Kabc")
  • ```
  • In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing the rest, so the output is just `abc`.
  • ---
  • Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):
  • ```python
  • # external lib, install it with "pip install colorama"
  • import colorama
  • colorama.just_fix_windows_console()
  • print("this is some text", end="\r")
  • print(f"{colorama.ansi.clear_line()}abc")
  • ```
#3: Post edited by user avatar hkotsubo‭ · 2023-10-09T14:30:53Z (about 1 year ago)
  • The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:
  • ```python
  • print("this is some text", end="\r")
  • print("abc")
  • ```
  • Testing this in a Linux terminal, the output is:
  • ```none
  • abcs is some text
  • ```
  • But I guess it should be only "abc", right? The rest of this answer assumes that that's the case.
  • IMO, the following isn't a pretty solution, but anyway: you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from `stdin`:
  • ```python
  • last_line = ''
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(' ' * len(last_line), end='\r') # spaces to "clear" the line
  • print(line, end='\r')
  • last_line = line
  • print() # just to print a newline after the last line
  • ```
  • Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).
  • ---
  • A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line before printing the next message:
  • ```python
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(f"\033[K{line}", end='\r')
  • print()
  • ```
  • With this solution, the first example will be:
  • ```python
  • print("this is some text", end="\r")
  • print("\033[Kabc")
  • ```
  • In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing `abc`.
  • ---
  • Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):
  • ```python
  • import colorama
  • colorama.just_fix_windows_console()
  • print("this is some text", end="\r")
  • print(f"{colorama.ansi.clear_line()}abc")
  • ```
  • The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:
  • ```python
  • print("this is some text", end="\r")
  • print("abc")
  • ```
  • Testing this in a Linux terminal, the output is:
  • ```none
  • abcs is some text
  • ```
  • But I guess it should be only "abc", right? The rest of this answer assumes that that's the case.
  • IMO, the following isn't a pretty solution, but anyway: you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from `stdin`:
  • ```python
  • last_line = ''
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(' ' * len(last_line), end='\r') # spaces to "clear" the line
  • print(line, end='\r')
  • last_line = line
  • print() # just to print a newline after the last line
  • ```
  • Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).
  • ---
  • A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line before printing the next message:
  • ```python
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(f"\033[K{line}", end='\r')
  • print()
  • ```
  • With this solution, the first example will be:
  • ```python
  • print("this is some text", end="\r")
  • print("\033[Kabc")
  • ```
  • In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing the rest, so the output is just `abc`.
  • ---
  • Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):
  • ```python
  • import colorama
  • colorama.just_fix_windows_console()
  • print("this is some text", end="\r")
  • print(f"{colorama.ansi.clear_line()}abc")
  • ```
#2: Post edited by user avatar hkotsubo‭ · 2023-10-09T13:28:05Z (about 1 year ago)
  • The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:
  • ```python
  • print("this is some text", end="\r")
  • print("abc")
  • ```
  • Testing this in a Linux terminal, the output is:
  • ```none
  • abcs is some text
  • ```
  • Of course you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from stdin:
  • ```python
  • last_line = ''
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(' ' * len(last_line), end='\r') # spaces to "clear" the line
  • print(line, end='\r')
  • last_line = line
  • print() # just to print a newline after the last line
  • ```
  • Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).
  • ---
  • A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line:
  • ```python
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(f"\033[K{line}", end='\r')
  • print()
  • ```
  • With this solution, the first example will be:
  • ```python
  • print("this is some text", end="\r")
  • print("\033[Kabc")
  • ```
  • In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing `abc`.
  • ---
  • Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):
  • ```python
  • import colorama
  • colorama.just_fix_windows_console()
  • print("this is some text", end="\r")
  • print(f"{colorama.ansi.clear_line()}abc")
  • ```
  • The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:
  • ```python
  • print("this is some text", end="\r")
  • print("abc")
  • ```
  • Testing this in a Linux terminal, the output is:
  • ```none
  • abcs is some text
  • ```
  • But I guess it should be only "abc", right? The rest of this answer assumes that that's the case.
  • IMO, the following isn't a pretty solution, but anyway: you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from `stdin`:
  • ```python
  • last_line = ''
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(' ' * len(last_line), end='\r') # spaces to "clear" the line
  • print(line, end='\r')
  • last_line = line
  • print() # just to print a newline after the last line
  • ```
  • Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).
  • ---
  • A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line before printing the next message:
  • ```python
  • for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
  • print(f"\033[K{line}", end='\r')
  • print()
  • ```
  • With this solution, the first example will be:
  • ```python
  • print("this is some text", end="\r")
  • print("\033[Kabc")
  • ```
  • In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing `abc`.
  • ---
  • Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):
  • ```python
  • import colorama
  • colorama.just_fix_windows_console()
  • print("this is some text", end="\r")
  • print(f"{colorama.ansi.clear_line()}abc")
  • ```
#1: Initial revision by user avatar hkotsubo‭ · 2023-10-09T13:24:22Z (about 1 year ago)
The solution proposed by the [other answer](/posts/289907/289919#answer-289919) works, but there's a corner case. If the last message is shorter than the previous one, you might not get what you want. Example:

```python
print("this is some text", end="\r")
print("abc")
```

Testing this in a Linux terminal, the output is:

```none
abcs is some text
```

Of course you could workaround this by printing lots of spaces before the next message (using the previous message's length to know how many spaces to use). For example, assuming the script is reading from stdin:

```python
last_line = ''
for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
    print(' ' * len(last_line), end='\r') # spaces to "clear" the line
    print(line, end='\r')
    last_line = line
print() # just to print a newline after the last line
```

Even though, in the case of a previous message bigger than the last one, there'll be extra spaces in the end (not visible, but still extra characters, which might or might not make a difference, depending on what you need).

---

A better solution, IMO, is to use the [ANSI escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code) `\033[K` to clear the line:

```python
for line in map(str.rstrip, sys.stdin): # read from stdin, strip new line at the end
    print(f"\033[K{line}", end='\r')
print()
```

With this solution, the first example will be:

```python
print("this is some text", end="\r")
print("\033[Kabc")
```

In this case, the output isn't `abcs is some text` anymore, as the ANSI escape sequence clears the line before printing `abc`.

---

Older versions of Windows don't support ANSI escape sequences, and for those, one solution is to use an external library, such as [Colorama](https://pypi.org/project/colorama/):

```python
import colorama
colorama.just_fix_windows_console()

print("this is some text", end="\r")
print(f"{colorama.ansi.clear_line()}abc")
```