Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python for File Systems Manipulation Consistency

Python - Consistency.py

Can someone help solve this code.

import os
import re

# Filenames consist of a username (alphanumeric, 3-12 characters)
# and a date (four digit year, two digit month, two digit day),
# and an extension. They should end up in the format
# year-month-day-username.extension.

# Example: kennethlove2-2012-04-29.txt becomes 2012-04-29-kennethlove2.txt

def cleanup(path):
    for dirs, dirp, files in os.walk('path'):
        for fil in files:
            result = re.search(r'([a-z0-9]+)(-)(\d{4}-\d{2}-\d{2})([.a-z]+)', fil)
            if result:
               replace = result.group(3) + result.group(2) + result.group(1) + result.group(4)
               os.rename(os.path.join(path, fil),os.path.join(path, replace))

Kenneth Love Chris Freeman

Please help with this.

3 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

You are very close! The argument to os.walk() should not be in quotes. With the quotes you are parsing the literal string "path" and not the argument passed into the function.

Nice use of regex!

Sadly, Kenneth Love is not currently teaching at Treehouse so he might not respond to direct tags.

Post back if you have more questions. Good luck!!

Chris Freeman Thanks for replying. I have removed the quotes from os.walk() and now I am getting - "Bummer! Found some non-cleaned up file names" error. Can you please have a look.

Sad to hear - Kenneth is not teaching here and I am not sure to whom to tag in his absence.

Sometimes it becomes too hard to find the details and we lose interest in learning.

I am holding on to this challenge from past 4 days.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

If I use your original code above and only remove quotes from os.walk(path) it passes the challenge.

Have you changed something else in the code?

You can tag Craig Dennis or Ken Alger (both are current instructors in Python topics). You can also tag me (a long time student) if you wish!

Chris Freeman I did the same thing what I did previously. It worked now. Thanks!

4,891 Points

Love how your solution uses RegEx groups and re-arranges the parts to correctly format. Very clever.