1

I am using Python 3.8.1 on Windows 10 and VSCode.

I have a simple program which writes a lot of text into a .txt file. The final file size of the .txt file is around 7MB so it takes quite a bit of time for writing the text into the file.

I have read other questions about progress bars but if I understood correctly, they need you to know how much time an operation requires and you have to determine the estimated time for the progress bar yourself, instead of the progress bar itself to automatically figure out how long an operation takes. On top of that, many of them focus on for/while loops and my script does not have a loop, it is just writing text in a file:

my_text = "Hello World, Hello World, Hello World"

text_file = open("Output.txt", "w", encoding = 'utf-8')
text_file.write(my_text)
text_file.close()

My question: is it possible to write code for a progress bar that finds out how long it takes for my program to write to a text file in seconds, and display that in the progress bar in percents and time remaining?

(e.g. Progress 40%/100% ======---------, 3.5MB/8MB written to file, 44 seconds remaining)

If this is not possible, how do I determine myself how long it takes to write the text to a file? Is there a script or package for writing a separate program for that?

jeppoo1
  • 650
  • 1
  • 10
  • 23
  • You could always use the time module if you'd like to time your wites to the file. However, if you really do need a progress bar, then you can always look for external python packages. – Anirudh Panchangam Jan 13 '20 at 12:21
  • Have a look at the tqdm library https://github.com/tqdm/tqdm . Might help you – AMargheriti Jan 13 '20 at 12:29
  • Thank you! I am familiar with those packages, and would still like to have an answer to my question about the automatic timing and usage without for or while loops *"is it possible to write code for a progress bar that **AUTOMATICALLY** finds out how long it takes for my program to write to a text file in seconds, and display that in the progress bar in percents and time remaining?"* Does sleep work automatically or do I need to manually determine the timing (like "this operation takes 50 seconds so I need to set sleep to value X")? – jeppoo1 Jan 13 '20 at 12:38
  • I don't have a loop, it's a single write command. In that case is it possible? ` with open(path2file, 'wb+') as f: # with open(path2file, 'w+') as f: f.write(data.read())` – Charlie Parker Nov 12 '22 at 02:29

1 Answers1

1

This might help you. Various approaches are outlined in the answers.

Python Progress Bar

  • Thank you. I saw that page but did not find an answer to my question. Do you know an answer to **"is it possible to write code for a progress bar that AUTOMATICALLY finds out how long it takes for my program to write to a text file in seconds, and display that in the progress bar in percents and time remaining?"**? – jeppoo1 Jan 13 '20 at 12:39
  • So, the question here is to figure out how much time a write operation takes? – Alphy Sebastian Jan 14 '20 at 05:11
  • Yes, **and also display that in the progress bar in percents and time remaining.** – jeppoo1 Jan 14 '20 at 06:53
  • I don't have a loop, it's a single write command. In that case is it possible? ` with open(path2file, 'wb+') as f: # with open(path2file, 'w+') as f: f.write(data.read())` – Charlie Parker Nov 12 '22 at 02:29