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

Make a function named delete_by_date. It should take date string like 2015-10-31 and delete any files in the "backups" l

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".

purging.py
import os
Steven Parker
Steven Parker
231,096 Points

It looks like you just quoted the instructions and haven't written any code yet.

At least give it your best "good faith" try, and then ask for help if you still have trouble. Be sure to describe what part you need help with.

merc1er
merc1er
6,329 Points
import os

# 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".

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

2 Answers

Tom Nguyen
Tom Nguyen
33,500 Points

My solution for task 1 and task 2

purging.py
import os

# 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".

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


def delete_by_user(name):
    for f in os.listdir(os.getcwd() + '/backups'):
        if name in f:
            os.remove('backups/' + f)
Nicolai Christensen
Nicolai Christensen
2,996 Points

Just passed task 1+2 with this - Tried googling for a way to use wildcards with os. and stumbled upen glob

import os
import glob

def delete_by_date(file_date):
    path = os.getcwd() + "/backups/"
    files = glob.glob('{}{}*'.format(path, file_date))
    for remove in files:
        os.remove(remove)

def delete_by_user(user_name):
    path = os.getcwd() + "/backups/"
    files = glob.glob('{}*{}*'.format(path, user_name))
    for remove in files:
        os.remove(remove)