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

Nikolay Nogin
Nikolay Nogin
22,154 Points

Help! ('posix.DirEntry' object has no attribute 'startswith' )

I've got this AttributeError: 'posix.DirEntry' object has no attribute 'startswith'. I'm on windows. And I only can see that it's something with OS on the server....

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):
    files = list(os.scandir(path))
    for item in files:
            filename, file_extension = os.path.splitext(os.path.abspath(item))
            item1 = item.name.replace(file_extension, '').split('-')
            item1 = item1[1]+'-'+item1[2]+'-'+item1[3]+'-'+item1[0]+file_extension
            os.renames(item, item1)

1 Answer

Nikolay Nogin
Nikolay Nogin
22,154 Points

If somebody is interested, I replaced a couple of lines and it passed!

def cleanup(path):
    files = os.listdir(path)
    for item in files:
            file_extension = '.'+ item.split('.')[1]
            item1 = item.replace(file_extension, '').split('-')
            item1 = item1[1]+'-'+item1[2]+'-'+item1[3]+'-'+item1[0]+file_extension
            os.rename(os.path.join(path, item), os.path.join(path, item1))