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

Need Help

What am i doing wrong?

purging.py
import os
import re
import datetime

def delete_by_date(date_string):
    v = date_string.split('-')
        if len(v) == 4 and v[:3].join('-') == ['%Y-%m-%d']
            for each_file in os.listdir('backups'):
                if v[:3].join('-') is in each_file:
                    os.remove(each_file)
        else:
            pass

1 Answer

You are overthinking it.

  • You do not need regular expressions for this problem, though you could if you want.
  • You also don't need an if condition. Assume the problem always provides valid input.
  • join is a function on a string, and it takes a list, not the other way around.
  • When you are removing a file, you need to specify that the file is in the directory backups, because the files the challenge wants you to delete are all in there.

Final code:

def delete_by_date(date):
    for filename in os.listdir("backups"):
        if date in filename:
            os.remove('backups/' + filename)

I hope this helps! :grin:

Happy coding! :zap: ~Alex