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 Manipulation Consistency

Python - Consistency.py

Can someone help solve this code.

consistency.py
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
MOD
Chris Freeman
Treehouse Moderator 68,423 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 68,423 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!

justlevy
justlevy
6,325 Points

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