I ran into this problem recently. I downloaded several files at the same time, and I had to create a timeout if the download failed.
The code checks the file names in a download directory every second and exits when they are complete or if it takes more than 20 seconds to complete them. The returned load time was used to verify the success of the downloads or the timeout.
import time import os def download_wait(path_to_downloads): seconds = 0 dl_wait = True while dl_wait and seconds < 20: time.sleep(1) dl_wait = False for fname in os.listdir(path_to_downloads): if fname.endswith('.crdownload'): dl_wait = True seconds += 1 return seconds
I believe this only works with Chrome files, as they end with the .crdownload extension. There may be a similar way of checking in other browsers.
Edit: I recently changed the way this function is used for times when .crdownload does not appear as an extension. Essentially, it just waits for the correct number of files.
def download_wait(directory, timeout, nfiles=None): """ Wait for downloads to finish with a specified timeout. Args ---- directory : str The path to the folder where the files will be downloaded. timeout : int How many seconds to wait until timing out. nfiles : int, defaults to None If provided, also wait for the expected number of files. """ seconds = 0 dl_wait = True while dl_wait and seconds < timeout: time.sleep(1) dl_wait = False files = os.listdir(directory) if nfiles and len(files) != nfiles: dl_wait = True for fname in files: if fname.endswith('.crdownload'): dl_wait = True seconds += 1 return seconds
Austin mackillop
source share