By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

is there a better way to walk a file system?

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ina wrote:
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
Jul 19 '05 #2

P: n/a
"Peter Hansen" wrote:
ina wrote:
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

Jul 19 '05 #3

P: n/a
George Sakkis wrote:
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:
class A(object): .... 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

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.