Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python for File Systems Navigation Checking Directory Contents

Can't get full output from tree.py, it only returns 'zero' for the target of Start.

Terminal (Mac) output: Wills-Air:python willanderson$ python3 tree.py Projects contains 0 files with a combined size of 0 bytes Wills-Air:python willanderson$

Code: import os

def treewalker(start): total_size = 0 total_files = 0

for root, dirs, files in os.walk(start):
    subtotal = sum(
        os.path.getsize(os.path.join(root, name)) for name in files
    )
    total_size += subtotal
    file_count = len(files)
    total_files += file_count
    print(root, "consumes", end=" ")
    print(subtotal, end=" ")
    print("bytes in", file_count, "non-directory files")

print(start, "contains", total_files, "files with a combined size of", total_size, "bytes")

this project is located at /Users/willanderson/Projects/python/tree.py

treewalker ('Projects')

I don't get it, why doesn't it work like the on-screen code (I think it's an exact copy)??

1 Answer

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

You're running the in the directory you want to evaluate.

Change this line:

treewalker('Projects')

to:

treewalker('../Projects') 

Let me know if that fixes your problem.