From b5f0912e133472e6358abe40b35feedbe259617c Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Tue, 9 Mar 2021 17:35:23 +0100 Subject: [PATCH] README.md: minimize scope of opened file in examples Prompted-by: Joao Rodrigo Windisch Olenscki --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 846f1fa..f93317c 100644 --- a/README.md +++ b/README.md @@ -161,26 +161,26 @@ The package can also be used as a library: # convert all files ending in .jpg inside a directory dirname = "/path/to/images" + imgs = [] + for fname in os.listdir(dirname): + if not fname.endswith(".jpg"): + continue + path = os.path.join(dirname, fname) + if os.path.isdir(path): + continue + imgs.append(path) with open("name.pdf","wb") as f: - imgs = [] - for fname in os.listdir(dirname): - if not fname.endswith(".jpg"): - continue - path = os.path.join(dirname, fname) - if os.path.isdir(path): - continue - imgs.append(path) f.write(img2pdf.convert(imgs)) # convert all files ending in .jpg in a directory and its subdirectories dirname = "/path/to/images" + imgs = [] + for r, _, f in os.walk(dirname): + for fname in f: + if not fname.endswith(".jpg"): + continue + imgs.append(os.path.join(r, fname)) with open("name.pdf","wb") as f: - imgs = [] - for r, _, f in os.walk(dirname): - for fname in f: - if not fname.endswith(".jpg"): - continue - imgs.append(os.path.join(r, fname)) f.write(img2pdf.convert(imgs))