Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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):

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
217,627 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.