README.md: minimize scope of opened file in examples

Prompted-by: Joao Rodrigo Windisch Olenscki
This commit is contained in:
Johannes Schauer Marin Rodrigues 2021-03-09 17:35:23 +01:00
parent 213a6af41f
commit b5f0912e13
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

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