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

Not quite sure why the File Not Found Error

I'm not sure what I am doing wrong here. I took the approach to rebuild from an empty string. I think my os module stuff is right - but not completely sure.

Any help would be appreciated.

consistency.py
import os

# 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):
    # your code here
    files = os.listdir(path)
    for file in files:
        new_name = ""
        tmp = file.split("-")
        # print(tmp)
        ext = tmp[-1].split(".")
        # print(ext)
        new_name += tmp[1] + "-" + tmp[2] + "-" + ext[0] + "-" + tmp[0] + "." + ext[1]
        # print(new_name)
        os.renames(file, new_name)

2 Answers

Ok, I figured it out.

Last line of my function should have been this:

os.renames(os.path.join(path, file), os.path.join(path, new_name))

Once I made that adjustment - the tests passed.

justlevy
justlevy
6,325 Points

I'm adding to this thread because my code is somewhat similar and it's not passing the challenge. Any ideas ( Chris Freeman - Python master)? I'm puzzled since this code works for me locally (Visual Studio Code, Windows).

John Davis - I like how you combined os.path.join in os.renames.

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):
    dirs = os.listdir(path)
    for files in dirs:
        username, yyyy, mm, dd, ext = re.split('\.|-', files)
        new_files = yyyy + '-' + mm + '-' + dd + '-' + username + '.' + ext
        old_paths = os.path.join(path, files)
        new_paths = os.path.join(path, new_files)
        os.rename(old_paths, new_paths)


    # change original filename to preferred filename (regex, split ?)
    # rename files (os.renames?) 


cleanup('consistency')
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Hey justlevy, your code passes for me if I remove last line. the call to cleanup

justlevy
justlevy
6,325 Points

Thank you Chris Freeman.

For some reason, I assumed we had to call the function.