PDF session/allmypdfs.py: Difference between revisions
(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( | # # print(item) | ||
# mypdfs.append(item) | |||
# | # | ||
# | # # print(mypdfs) | ||
# print(f"I have { | # 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) | # 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.") | |||
</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.")