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 trialAlbert Egberg
7,786 PointsCan someone explain to me what I am doing wrong with my code?
I don't fully understand the things I am using: like os.scandir.
here's the question: Make a function named delete_by_date. It should take date string like 2015-10-31 and delete any files in the "backups" local directory that have that date in their filename. Just like the last challenge, the files will be named in the format "year-month-day-username.extension".
import re
import os
def delete_by_date(str_date):
for file in os.scandir("backups"):
if str_date == re.search(r'\d{4}-\d{2}-\d{2}', file):
os.remove(file.path)
1 Answer
Steven Parker
231,122 PointsI noticed these issues:
- the "file" can't be searched with a regex, you probably meant to use "file.path"
- a
re.search
returns a match object which would not compare with a string - the "group" method could be used to extract the matching string from the object
- this task could also be done without regex using string operations
Albert Egberg
7,786 PointsAlbert Egberg
7,786 PointsThank you, Parker! :)