Skip to content

Instantly share code, notes, and snippets.

@cathalgarvey
Forked from anonymous/countfiles
Last active December 10, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathalgarvey/4503567 to your computer and use it in GitHub Desktop.
Save cathalgarvey/4503567 to your computer and use it in GitHub Desktop.
A little script I wrote to perform a quick census on my music library, to help me identify which artists/albums contain the most mp3s/m4as. This was intended to help me replace my music library with oggs, but could be used for all sorts of other handy things, too.
#!/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 filen[:2] == "._":
# Necessary because Rhythmbox (at least) populates some library folders with hidden
# files of format "._{filename}.mp3" for each mp3 in that folder: this can screw up
# the count when you simply count how many mp3s are present.
continue
if filetype == filen.lower()[len(filen)-len(filetype):]:
# Above len/len voodoo just compares filetype to only the last letters of filenames.
# This avoids a file containing the file extension by accident being counted.
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