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

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
221,291 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,487 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)