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: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -161,26 +161,26 @@ 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"
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: 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)) 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"
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: 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)) f.write(img2pdf.convert(imgs))