Trying to make a program to count all unique files in a directory, I did manage to get it working for just top level folders, how can I also capture subfolder?
import glob, os
psm = 0
prt = 0
asm = 0
dft = 0
os.chdir("Z:\TopFolder")
for file in glob.glob("*.asm"):
asm = 1
print('Assemblies: ', asm)
for file in glob.glob("*.psm"):
psm = 1
print('Sheet Metal Parts: ', psm)
for file in glob.glob("*.prt"):
prt = 1
print('Regular Parts: ', prt)
for file in glob.glob("*.dft"):
dft = 1
print('Drafts: ', dft)
total = psm prt asm dft
print('Total unique files (No .PDF, .CFG or Docs: ', total)
CodePudding user response:
Here's a sketch using dictionaries and os.walk
.
import os
extensions = {
".asm": "Assemblies",
".dft": "Drafts",
".prt": "Regular Parts",
".psm": "Sheet Metal Parts",
}
count = {x: 0 for x in extensions.keys()}
for dirname, dirs, files in os.walk(r"Z:\TopFolder"):
for file in files:
for ext in count.keys():
if file.endswith(ext):
count[ext] = 1
total = 0
for ext in count.keys():
print("%s: %i" % (extensions[ext], count[ext]))
total = count[ext]
print("Total:", total)