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 Purging

Found some non-cleaned up file names

Not sure what they mean by "Found some non-cleaned up file names"

If date = "2015-10-31" and interpreting that we are already in the 'backups' folder. I came up with this:

import os
import re

def delete_by_date(date):
    for files in os.scandir():
        if files.name.startswith(date):
            os.rename(files.name)

Doing this code in vs.code. It does delete all files the start with whatever date that is giving to the 'date' argument. Like '2015-10-31'.
Assuming all files have the format "year-month-day-username.extension", I also used files.name.contains(date), in place of files.name.startswith(date), but I got the same error.

Sorry, I typed 'rename'. I did have 'remove' but I see my error though. I had to give the full path for os.remove().......os.remove(f'backups/{files.name}'). Also, with your help, using os.scandir('backups'). Not having the full path for os.remove really messed me up. Also I added files.is_file() to the if condition to make sure that i'm not trying to delete any folder with 'date'.

Thank you much!

1 Answer

Steven Parker
Steven Parker
231,275 Points

By default, scandir will examine the current directory, but the instructions say the function should work on files in the "backups" local directory instead.

Also, the instructions say to delete matching files instead of renaming them.