This recipe manually counts the number of sub-directories and files can be found in a directory. It was an experimental program from long ago used to learn more about Python. This is committed for archival to be run under Python 2.5 or later versions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import os, sys
def main():
if len(sys.argv) - 1:
engine(' '.join(sys.argv[1:]))
else:
print os.path.basename(sys.argv[0]), '<directory>'
def engine(path):
directories = files = 0
for information in os.walk(path):
directories += len(information[1])
files += len(information[2])
print 'Directories =', directories
print 'Files =', files
if __name__ == '__main__':
main()
|
Download
Copy to clipboard
Thanks for publishing this script. I'm just getting started. Being able to see simple scripts that do things that are somewhat easy to grasp is very helpful for my learning.