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

Jonathan Beard
Jonathan Beard
1,681 Points

Need assistance in splitting date and names for function

I wrote the below code, but after running it realized it will just keep renaming them based on the new order, rather than naming them correctly and verifying the format. I have no clue how to proceed. The videos didn't go over how to split and verify the string.

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
path = list(os.listdir(path))

def cleanup(path):
    # your code here
    for file in files:
    name_file = file.split('-')
    file_ext = name_file[3].split('.')
    new_filename = name_file[1] + '-' + name_file[2] + '-' + file_ext[0] + '-' +name_file[0] + '.' + file_ext[1]
    os.rename(path + file, path + new_filename)

cleanup(path)