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

Help with delete_by_date. Works fine on my local machine. TypeError: remove: illegal type for path parameter in the Tree

Getting the following output from the Treehosue console when I run this code.

Works perfectly on my local machine

purging.py
import os
import re

def delete_by_date(date):
    regex = '(?P<date>\d{4}\-\d{2}\-\d{2})'
    for item in os.scandir('backups'):
        match = re.search(regex, item.name)
        for match in match.groups():
            if match == date:
                os.remove(item)

2 Answers

Steven Parker
Steven Parker
231,236 Points

The value in "item" was set from a call to "os.scandir", so it contains an "os.DirEntry" object. But the "os.remove" function takes a path argument. You might get that from object's "path" property.

Did this actually remove items on your local system? Or did it just run when there were no matching items?

HI Steven,

Yes I set up a dummy 'backups' folder on my system and it deleted the files with the corresponding date.

Will see if I can modify the code so I'm passing a path into os.remove()

Thanks for the feedback!