PDF session/allmypdfs.py: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<syntaxhighlight lang="python"> import os # ------ # myfolder = "/home/mb/Downloads" # # items = os.listdir(myfolder) # print(items) # # for item in items: # if item.endswith(".pdf"): # print("this is a PDF: " + item) # # howmany = len(items) # print(f"I have { howmany } PDFs in myfolder: { myfolder }") # ------ # # myfolder = "/home/mb/" # # pdfs = [] # # for root, dirs, files in os.walk(myfolder): # # print("-------") # # print("root:", root) #...")
 
No edit summary
 
Line 1: Line 1:
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import os
import os
# ------


# myfolder = "/home/mb/Downloads"
# myfolder = "/home/mb/Downloads"
#
#
# # items is a list of strings (which are filenames)
# # items = ["filename.pdf", "file.png", "lol.gif"]
# items = os.listdir(myfolder)
# items = os.listdir(myfolder)
# print(items)
# # print(items)
# # print(type(items))
# # print(len(items))
#
#
# mypdfs = []
# for item in items:
# for item in items:
#    if item.endswith(".pdf"):
#    if item.endswith(".pdf"):
#        print("this is a PDF: " + item)
#        # print(item)
#        mypdfs.append(item)
#
#
# howmany = len(items)
# # print(mypdfs)
# print(f"I have { howmany } PDFs in myfolder: { myfolder }")
# 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)


# ------
#
# myfolder = "/home/mb/"
#
# 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)
# print(pdfs)
#
# howmany = len(pdfs)
# print(f"Oh wow, I have { howmany } PDFs on my computer!!")


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


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


# ------
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 16:32, 28 January 2025

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.")