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

Not sure why this doesn't work. Seems to work fine on my machine.

Is there and edge case that I am missing here? I assume all the date inputs are valid dates, as I'm not told what error to raise if the date is invalid. I tried this on my machine and it seems to work just fine for either date format. Any help is appreciated.

backup.py
import os

def create_daily_dir (date_str):
    a, b, c = date_str.split("-")
    if len(a) == 4:
        y, m, d = int(a), int(b), int(c)
    else:
        m, d, y, = int(a), int(b), int(c)   
    dir_name = "{}-{:02d}-{:02}".format(y, m, d)   

    os.makedirs(dir_name, exist_ok=True)

1 Answer

Ryan S
Ryan S
27,276 Points

Hi leakedmemory,

Nice solution. You are very close. I think you are being thrown off by the same thing that I was.

The 'financial' directory is not the current working directory, it is inside the current working directory. I misread this instruction at first.

So you will just need to make sure to supply the full path, eg., "financial/YYYY-MM-DD", in your final line. Other than that, your code works.

import os

def create_daily_dir (date_str):
    a, b, c = date_str.split("-")
    if len(a) == 4:
        y, m, d = int(a), int(b), int(c)
    else:
        m, d, y, = int(a), int(b), int(c)   
    dir_name = "{}-{:02d}-{:02}".format(y, m, d)   

    os.makedirs(os.path.join('financial', dir_name), exist_ok=True)

That did the trick. Thanks!