Python - count file by extension

Dell Precision m65 notebook
February 9, 2010 at 19:33:21
Specs: Windows XP

I was trying to write a python script to take in a directory, recursive scan it for all files, keep track the number of files with a particular extension and print how many files of each extension type there were.

Unfortunately I'm getting the following error:

Traceback (most recent call last):
  File "H:\", line 30, in <module>
    extensionarray = check_format(filearray)
  File "H:\", line 18, in check_format
    val = index(result1, extension)
  File "H:\", line 13, in index
    return next((i for i in xrange(len(seq)) if f(seq[i])), 'None')
  File "H:\", line 13, in <genexpr>
    return next((i for i in xrange(len(seq)) if f(seq[i])), 'None')
TypeError: 'str' object is not callable

I would appreciate any help, this was what I have so far:

import os, sys

def get_all_files(srchpath):
	allfiles = []
	for root, dirs, files in os.walk(srchpath):
		for file in files:
			pathname = os.path.join(root, file)
			extension = os.path.splitext(file)[1]
			allfiles.append([file, pathname, extension])
	return allfiles
def index(seq, f):
    return next((i for i in xrange(len(seq)) if f(seq[i])), 'None')
def check_format(thefiles):
	result1 = []
	for filename, pathname, extension in thefiles:
		val = index(result1, extension)
		if val == 'None':
			result1.append([extension, 0])
			result1[val][1] += 1
	return result1

def print_array(thearray):
	for val in range(1, len(thearray)):
		print thearray[val]

filearray = get_all_files(sys.argv[1])
extensionarray = check_format(filearray)

** thanks nbrane for catching my first mistake!

See More: Python - count file by extension

Report •

February 9, 2010 at 20:57:31
i ran pydoc os.path on my isp (ubunto) and it had splitext instead of splittext.
might try changing splittext to splitext in this line:
extension = os.path.splitext(file)[1]

Split the extension from a pathname. Extension is everything from the
last dot to the end. Returns "(root, ext)", either part may be empty.

Report •

February 9, 2010 at 22:31:06
thanks again for the help!

Report •

February 9, 2010 at 23:21:22
you, me both brother - you're an order of magnitude beyond me since i know zero - but i am interested to learn. You have made a start!
what is f, and where is it defined or established or assigned?
f(seq[i])), 'None')
if f is undefined, interp. might be looking for a function "f". but my take on it is that f is supposed to be either an array of, or a string of extensions. i don't know how arrays expressed in python: whether [] or (). might be you are not expressing the array definition as py wants it?
my guess is the critical-issue is item "f".

Report •

Related Solutions

February 10, 2010 at 20:20:27
I changed my plans a little on it, what you are looking at was a search I was using to scan the array to see if something was present.

I decided to change my approach to avoid that hassle and just use dictionaries.

I am shooting the output into a pie chart for easy viewing so you will need the matplot and numpy libs if you want to run it straight.

If you just want the output you can uncomment the last print statement and comment out the last four lines.

import os, sys
from pylab import *
from sys import argv, exit

def get_all_files(srchpath):
	allfiles = []
	for root, dirs, files in os.walk(srchpath):
		for file in files:
			pathname = os.path.join(root, file)
			ext = os.path.splitext(file)[1]
			allfiles.append([file, pathname, ext])
	return allfiles
def get_these_files(srchpath):
	somefiles = []
	dirList = os.listdir(srchpath)
	for f in dirList:
		pathname = os.path.join(srchpath, f)
		ext = os.path.splitext(f)[1]
		somefiles.append([f, pathname, ext])
	return somefiles
## excellent page on using dictionaries in python
def check_ext(thefiles):
	d = dict()
	for filename, pathname, ext in thefiles:
		if ext not in d:
			d[ext] = 1
			d[ext] += 1
	return d

def print_ext(d):
	for i in d:
		print i, d[i]

def type_num(d):
	type, num = [], []
	for i in d:
	return (type, num)

aPath = sys.argv[1]
aName = os.getenv('COMPUTERNAME')

if len(argv) > 2:
	if sys.argv[2] == '-r':
		getFiles = get_all_files(aPath)
		print 'Improper command'
	getFiles = get_these_files(aPath)
extDict = check_ext(getFiles)

type, num = type_num(extDict)
pie(num, labels=type, shadow=True)
title('File breakdown for ' + aPath + '  on  ' + aName)

Report •

February 10, 2010 at 21:56:30
since i'm intending to try and learn at least a smidgen of python, i can use your script to help me get started.

Report •

Ask Question