is there a better way to walk a file system?

I

ina

I want to walk a folder structor and group all the files by extention.


Here is the code I put together is there a better way of doing this?
<code>
import os
folderKey = "Folders"
dicExt = {}
tDicKey = []
tDicKey.append(folderKey)
dicExt[folderKey] = []

walkPath = r"\\zek\C$\AST"

for d in os.walk(walkPath):
(dirpath, dirnames, filenames) = d
dicExt[folderKey].append(dirpath)
for fname in filenames:
fs = os.path.splitext(fname)
if dicExt.has_key(fs[-1]):
dicExt[fs[-1]].append(os.path.join(dirpath,fname))
else:
dicExt[fs[-1]] = []
dicExt[fs[-1]].append(fname)
tDicKey.append(fs[-1])


for k in tDicKey:
print k +" "+ str(len(dicExt[k]))
</code>
Right now it is just prints out the counts.

Thanks

Erin
 
P

Peter Hansen

ina said:
I want to walk a folder structor and group all the files by extention.

Here is the code I put together is there a better way of doing this?
<code>
[snip]

If you were to download Jason Orendorff's "path" module, which is a
convenient single-file package that vastly simplifies all manner of
dealings with directory and file names, I suspect your example code
could be reduced to less than half the number of lines it now uses, and
with a corresponding increase in readability and maintainability.

The answer to the question "is there a better way of doing this?" in
relation to paths is always "yes, use Jason Orendorff's path module".

-Peter
 
G

George Sakkis

Peter Hansen said:
ina said:
I want to walk a folder structor and group all the files by extention.
Here is the code I put together is there a better way of doing this?
<code>

[snip]

If you were to download Jason Orendorff's "path" module, which is a
convenient single-file package that vastly simplifies all manner of
dealings with directory and file names, I suspect your example code
could be reduced to less than half the number of lines it now uses, and
with a corresponding increase in readability and maintainability.

Well, it's exactly half non-blank lines (10) and still readable; of
course it can be compressed more but we're not doing perl here <wink>.

import sys
from path import path
from itertools import groupby

def getExtension(aPath):
return aPath.ext

root = len(sys.argv) > 1 and sys.argv[1] or '.'
for ext,iterFiles in groupby(sorted(path(root).walkfiles(),
key=getExtension),
getExtension):
print "%s: %d files" % (ext, len(tuple(iterFiles)))


By the way, from this example I discovered that properties cannot be
unbound, i.e. using path.ext instead of getExtension raises TypeError.
Couldn't/shouldn't Class.prop(instance) be allowed as equivalent of
instance.prop, just as methods ?
The answer to the question "is there a better way of doing this?" in
relation to paths is always "yes, use Jason Orendorff's path module".

-Peter

George
 
P

Peter Otten

George said:
By the way, from this example I discovered that properties cannot be
unbound, i.e. using path.ext instead of getExtension raises TypeError.
Couldn't/shouldn't Class.prop(instance) be allowed as equivalent of
instance.prop, just as methods ?

Use the property's __get__() method instead:
.... def __init__(self, value):
.... self._value = value
.... value = property(lambda self: self._value)
.... def __repr__(self): return "A(%r)" % self._value
....
a = A(42)
A.value.__get__(a) 42
sorted([A(42), A(2), A(4)], key=A.value.__get__)
[A(2), A(4), A(42)]

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top