Checks if file exists in working_directory otherwise tries to dowload the file, and optionally also tries to extract the file if format is ".zip" or ".tar" Parameters ----------- filename : str The name of the (to be) dowloaded file. working_directory : str A fol
(filename, working_directory, url_source, extract=False, expected_bytes=None)
| 2404 | |
| 2405 | |
| 2406 | def maybe_download_and_extract(filename, working_directory, url_source, extract=False, expected_bytes=None): |
| 2407 | """Checks if file exists in working_directory otherwise tries to dowload the file, |
| 2408 | and optionally also tries to extract the file if format is ".zip" or ".tar" |
| 2409 | |
| 2410 | Parameters |
| 2411 | ----------- |
| 2412 | filename : str |
| 2413 | The name of the (to be) dowloaded file. |
| 2414 | working_directory : str |
| 2415 | A folder path to search for the file in and dowload the file to |
| 2416 | url : str |
| 2417 | The URL to download the file from |
| 2418 | extract : boolean |
| 2419 | If True, tries to uncompress the dowloaded file is ".tar.gz/.tar.bz2" or ".zip" file, default is False. |
| 2420 | expected_bytes : int or None |
| 2421 | If set tries to verify that the downloaded file is of the specified size, otherwise raises an Exception, defaults is None which corresponds to no check being performed. |
| 2422 | |
| 2423 | Returns |
| 2424 | ---------- |
| 2425 | str |
| 2426 | File path of the dowloaded (uncompressed) file. |
| 2427 | |
| 2428 | Examples |
| 2429 | -------- |
| 2430 | >>> down_file = tl.files.maybe_download_and_extract(filename='train-images-idx3-ubyte.gz', |
| 2431 | ... working_directory='data/', |
| 2432 | ... url_source='http://yann.lecun.com/exdb/mnist/') |
| 2433 | >>> tl.files.maybe_download_and_extract(filename='ADEChallengeData2016.zip', |
| 2434 | ... working_directory='data/', |
| 2435 | ... url_source='http://sceneparsing.csail.mit.edu/data/', |
| 2436 | ... extract=True) |
| 2437 | |
| 2438 | """ |
| 2439 | |
| 2440 | # We first define a download function, supporting both Python 2 and 3. |
| 2441 | def _download(filename, working_directory, url_source): |
| 2442 | |
| 2443 | progress_bar = progressbar.ProgressBar() |
| 2444 | |
| 2445 | def _dlProgress(count, blockSize, totalSize, pbar=progress_bar): |
| 2446 | if (totalSize != 0): |
| 2447 | |
| 2448 | if not pbar.max_value: |
| 2449 | totalBlocks = math.ceil(float(totalSize) / float(blockSize)) |
| 2450 | pbar.max_value = int(totalBlocks) |
| 2451 | |
| 2452 | pbar.update(count, force=True) |
| 2453 | |
| 2454 | filepath = os.path.join(working_directory, filename) |
| 2455 | |
| 2456 | logging.info('Downloading %s...\n' % filename) |
| 2457 | |
| 2458 | urlretrieve(url_source + filename, filepath, reporthook=_dlProgress) |
| 2459 | |
| 2460 | exists_or_mkdir(working_directory, verbose=False) |
| 2461 | filepath = os.path.join(working_directory, filename) |
| 2462 | |
| 2463 | if not os.path.exists(filepath): |
no test coverage detected
searching dependent graphs…