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

FileNotFoundError: [Errno 2] No such file or directory: '/workdir/messy/messy'

Can someone help me this code?

Kenneth Love

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
    os.chdir(path)
    for f in os.listdir():
        f_name, f_ext = os.path.splitext(f)
        #print(f_name, f_ext)
        f_title, f_d1, f_d2, f_d3 = f_name.split('-')
        #print(f_title, f_d1, f_d2, f_d3)
        #print('{}-{}-{}-{}{}'.format(f_d1, f_d2, f_d3, f_title, f_ext))
        new_name = '{}-{}-{}-{}{}'.format(f_d1, f_d2, f_d3, f_title, f_ext)
        #print(new_name)
        os.rename(f, new_name)

1 Answer

It's likely either the path you're supplying doesn't exist (maybe a typo), OR, Python doesn't have the permissions to access the files at that path…