The question how to overwrite your last printed line has been asked before and answered (e.g. here), but it seems all the answers assume you overwrite your last line with something of similar length, for example in a progress indicator where "2 % done" overwrites "1 % done".
However, I'd like to print variable length statements, such as "Now reading file path/to/xxx/file.ext..."
, where the next filename may be shorter.
If I use the simple approach, and first print "Now reading ./path/with/many/long-named/subdirectories/version/LongFileNameAsWell.ext..."
, followed by "Now reading ./file.ext..."
, the result is a line that says
Now reading ./file.ext... ng-named/subdirectories/version/LongFileNameAsWell.ext
A workaround would be to append all my print-strings with spaces, but then I'd need to make sure I don't wrap around to a newline, while still having enough spaces to cover the entire filename. This all adds a lot of overhead, room for bugs, etc.
Is there a Pythonic way to do this?
For the record, I'm using Python 3.7 on Spyder with the IPython Console, on Windows