Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2013 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4485837 to your computer and use it in GitHub Desktop.
Save anonymous/4485837 to your computer and use it in GitHub Desktop.
A little script that recurses into the subdirectories of a specified folder and reports the numbers of filenames containing the specified file extension in each base-directory folder. I use it to find which of my Music library's artist folders contain the most mp3s and m4as. so I could focus on replacing those with oggs. Usage: $ countfiles ~/Mu…
#!/usr/bin/env python3
import os
from sys import argv
# Walk through folders recursively, list the full path and number of (extension) files found in each.
basefolder = os.path.expanduser(argv[1])
filetype = str(argv[2]).lower()
output = []
# New system:
for folder in os.listdir(basefolder):
filecounter = 0
for subf, childf, files in os.walk(os.path.join(basefolder,folder)):
for filen in files:
if filetype in filen.lower():
filecounter += 1
if filecounter:
#print(folder, "contains {0} mp3s.".format(filecounter))
output.append([filecounter, folder])
# As first list item is an integer, will be sorted by this. Largest last.
output.sort()
print("Count, Path")
for x in output[:]:
newx = str(x[0]) + "\t" + x[1]
output[output.index(x)] = newx
print("\r\n".join(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment