Home >Backend Development >Python Tutorial >How to Overwrite Previous Output for Real-Time FTP Download Progress Display?
Overwriting Previous Output for FTP Download Status
In the context of developing an FTP downloader, one may encounter the need to display the download progress in a single line that updates periodically. The following code snippet illustrates the initial approach:
ftp.retrbinary("RETR " + file_name, process) def process(data): print(os.path.getsize(file_name)/1024, 'KB / ', size, 'KB downloaded!') file.write(data)
However, this approach results in multiple lines of output that do not reflect the progress. To address this issue, we can leverage the end parameter in the print() function.
print(os.path.getsize(file_name)/1024+'KB / '+size+' KB downloaded!', end='\r')
In Python 3.x, the end parameter allows us to specify a character or string to append to the end of the printed line. By setting it to 'r', a carriage return, we return the cursor to the start of the current line without adding a newline. This effectively overwrites the previous output, enabling us to display the progress in a single, updating line.
For Python 2.6 , we can utilize the __future__ module to enable Python 3-style printing:
from __future__ import print_function # Code remains the same as in Python 3.x
The above is the detailed content of How to Overwrite Previous Output for Real-Time FTP Download Progress Display?. For more information, please follow other related articles on the PHP Chinese website!