How to read currently downloading file -Js OR Python

Let's say that I'm downloading a file and at the same time I want to read it but that the file content always get updated. For example let's assuming that I start read the file when the progress bar was at 60% is there a way to read and wait at the same time for the remaining part which is the 40% of the file content that is currently downloading.

I tired the normal reading method in python:

with open('downloadingfile.mp4', 'rb') as f:
   data = f.read()
   # remaining code which is not important

2 answers

  • answered 2021-03-08 21:22 Barmar

    If you know the expected file size, call read() in a loop until you get to that size.

    import time
    
    with open('downloadingfile.mp4', 'rb') as f:
        total_read = 0
        while total_read < file_size:
            data = f.read()
            if data:
                total_read += len(data)
                # process this chunk
            time.sleep(1)
    

  • answered 2021-03-08 21:36 Ahmed Ashrf

    you must first know that there is an advanced way for threading and this way big companies use but here I am going to show a threading way but it is not advanced try this code I don't know if it is going to work I am new in programming

    def Handel_Progress(self , blocknum , blocksize , totalsize):
        ## calculate the progress
        readed_data = blocknum * blocksize
    
        if totalsize > 0 :
            download_percentage = readed_data * 100 / totalsize
            self.setValue(download_percentage)
            processEvents()# before process even put the name of the application or if you are using python without make to it GUI I don't know what to do