# # this is how you write a comment: start the line with a '#' # # here's how you run this script: python countMp3s.py # # For example: python countMp3s.py /Users/mcarlin/Music # # import stuff #save me as countMp3sCommented.py from fnmatch import fnmatch import os import sys # make sure we got a path (like "/Users/mcarlin/Music") # at the command line if len(sys.argv) < 2: print "Please supply a path argument; this is the folder path to your music folder" exit(1) # this is the path path = sys.argv[1] # variables to count mp3s, folders, and weird folders total_mp3s = 0 total_folders = 0 total_weird_folders = 0 # walk through the folders and sub folders for (sub_path, folders, files) in os.walk(path): # in each directory, get the list of files, and filter for mp3s mp3files = [x for x in files if fnmatch(x, "*.mp3")] # add to the total mp3s and folders total_mp3s += len(mp3files) total_folders += 1 # if this directory has mp3s *and* sub folders, it's weird! if len(folders) > 0 and len(mp3files) > 0: print sub_path total_weird_folders += 1 # calculate the fraction of folders that are weird fraction_weird_folders = 0 if total_folders > 0: fraction_weird_folders = total_weird_folders / float(total_folders) # print all of the things! print "Total number of mp3s: %d" % total_mp3s print "Number of folders with mp3s and subfolders: %d" % total_weird_folders print "Total number of folders: %d" % total_folders print "Fraction of folders with mp3s and subfolders: %f" % fraction_weird_folders