README.md: minimize scope of opened file in examples

Prompted-by: Joao Rodrigo Windisch Olenscki
main
parent 213a6af41f
commit b5f0912e13
Signed by untrusted user: josch
GPG Key ID: F2CBA5C78FBD83E1

@ -161,7 +161,6 @@ The package can also be used as a library:
# convert all files ending in .jpg inside a directory # convert all files ending in .jpg inside a directory
dirname = "/path/to/images" dirname = "/path/to/images"
with open("name.pdf","wb") as f:
imgs = [] imgs = []
for fname in os.listdir(dirname): for fname in os.listdir(dirname):
if not fname.endswith(".jpg"): if not fname.endswith(".jpg"):
@ -170,17 +169,18 @@ The package can also be used as a library:
if os.path.isdir(path): if os.path.isdir(path):
continue continue
imgs.append(path) imgs.append(path)
with open("name.pdf","wb") as f:
f.write(img2pdf.convert(imgs)) f.write(img2pdf.convert(imgs))
# convert all files ending in .jpg in a directory and its subdirectories # convert all files ending in .jpg in a directory and its subdirectories
dirname = "/path/to/images" dirname = "/path/to/images"
with open("name.pdf","wb") as f:
imgs = [] imgs = []
for r, _, f in os.walk(dirname): for r, _, f in os.walk(dirname):
for fname in f: for fname in f:
if not fname.endswith(".jpg"): if not fname.endswith(".jpg"):
continue continue
imgs.append(os.path.join(r, fname)) imgs.append(os.path.join(r, fname))
with open("name.pdf","wb") as f:
f.write(img2pdf.convert(imgs)) f.write(img2pdf.convert(imgs))

Loading…
Cancel
Save