PDF session/allmypdfs.py

From XPUB & Lens-Based wiki
Revision as of 16:32, 28 January 2025 by Manetta (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
import os

# myfolder = "/home/mb/Downloads"
#
# # items is a list of strings (which are filenames)
# # items = ["filename.pdf", "file.png", "lol.gif"]
# items = os.listdir(myfolder)
# # print(items)
# # print(type(items))
# # print(len(items))
#
# mypdfs = []
# for item in items:
#     if item.endswith(".pdf"):
#         # print(item)
#         mypdfs.append(item)
#
# # print(mypdfs)
# print(f"In this folder { myfolder } I have { len(mypdfs) } pdfs.")
# print("In this folder" + myfolder + " I have " + str(len(mypdfs)) + "pdfs.")

# --------------

myfolder = "/home/mb/"
# myfolder = "/home/mb/Downloads"

pdfs = []

for root, dirs, files in os.walk(myfolder):
    print("-------")
    print("root:", root)
    print("folders:", dirs)
    print("files:", files)
    for file in files:
        if file.endswith(".pdf"):
            pdfs.append(file)

# print(pdfs)

howmany = len(pdfs)
print(f"Oh wow, I have { howmany } PDFs on my computer!!")

print("===========")

outputfile = open("allmypdfs.txt", "w")
# print(type(outputfile))
for pdf in pdfs:
    # print(pdf)
    outputfile.write(pdf)
    outputfile.write("\n")
outputfile.close()
print("Output file is written. You are done now.")