import os import requests from retrying import retry from io import BytesIO from PIL import Image import progressbar import concurrent.futures as concurrent
i = 0 total = len(image_urls) pbar = progressbar.ProgressBar(max_value=total).start() for task in concurrent.as_completed(tasks): url, file_path = tasks[task] try: task.result() i = i + 1 pbar.update(i) except Exception as exc: print('{} generated an exception: {}'.format(url, exc)) pbar.finish()
4. 遍历文件夹中所有文件
首先目录结构如下:
1 2 3 4 5 6 7
$ tree test test ├── 1.txt ├── 2.txt └── test2 ├── 3.txt └── 4.txt
使用os.walk()遍历test目录,代码如下:
1 2 3 4 5 6
root_dir = '/tmp/test' for root, dirs, files in os.walk(root_dir, topdown=True): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name))
deftimethis(func): """ Decorator that reports the execution time """ @wraps(func) defwrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(func.__name__, end-start)
if __name__ == "__main__": a = [1, 2] b = 3 print(f"'a' is {'iterable'if isiterable(a) else'not iterable'}") print(f"'b' is {'iterable'if isiterable(b) else'not iterable'}")