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 Daily Backup

Kamaren McCord
Kamaren McCord
11,236 Points

year-month-day formatting not working

Can't seem to figure out how to resolve this task! From what I understand this function will be receiving a string in the format of year-month-day or month-day-year by default and we are to create a directory within the current directory "./" with the year-month-day format.

I've created a simple check to see if the first part of the string is 4 digits ( a year ) or not. If it is not then we simply re-format the string into the correct pattern.

If the 1st part of the string is 4 digits then we can assume we have a year and the string is already in the year-month-day format.

TLDR; what's wrong with my logic? What am I not understanding about the question?

ERROR Shown: Bummer: Didn't find the right directory for a month-day-year date

and YES, I have tried to make multiple directories with both formats

backup.py
import os

def create_daily_dir(datestr):
    ds = datestr.split("-")
    if len(ds[0]) != 4:
        # reformat month,day,year to year,month,day
        os.mkdir(f"{ds[2]}-{ds[0]}-{ds[1]}")
        return
    # else datestr is already year month day
    os.mkdir(datestr)

1 Answer

Rachel Johnson
STAFF
Rachel Johnson
Treehouse Teacher

Hey Kamaren McCord you're off to a great start! Your logic is definitely there and that's a pretty smart way of getting the date formatted correctly. The only thing missing is that we should be creating this folder in a financial folder. Make sure you're using makedirs when creating a folder structure!

os.makedirs(f"financial/{datestr}")
Kamaren McCord
Kamaren McCord
11,236 Points

omg, thank you so much. I was just hitting a wall here. I see where i was missing the financial part of it. I had assumed the current directory WAS the financial folder and thus we just needed to create a new one with the date string.

I also see where we would need to use os.makedirs because we'd just get an error with os.mkdir if the subfolder "financial" didn't already exist and that os.makedirs is going to create this subfolder as needed.

Thanks again!