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

Hi, I dont know what I'm doing wrong

Can someone help me out?

purging.py
import os
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(username):
    for username in os.listdir(os.getcwd()):
        if username.is_file:
           os.remove(username.path)

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

This is the question

1 Answer

Trevor J
seal-mask
.a{fill-rule:evenodd;}techdegree
Trevor J
Python Web Development Techdegree Student 2,107 Points

Unless you are referring to Challenge Task 2 of 2 and not 1 You don't need the delete_by_user(), remove this method and try again.
Also there should not be a spcae after by in the method name.

 def delete_by _user(username):
    for username in os.listdir(os.getcwd()):
        if username.is_file:
           os.remove(username.path)

For Challenge Task 2 of 2 the delete_by_user method is the same just change date for username

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(username):
    for f in os.listdir(os.getcwd() + '/backups'):
        if username in f:
            os.remove('backups/' + f)

Or for a cheeky shortcut:

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(username):
    delete_by_date(username)