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

ben lewin
ben lewin
11,495 Points

What is wrong with the following?

I'm not getting the right response for a year-month-date output, any possible solutions?

Many thanks,

backup.py
import os
import re

def create_daily_dir(adate):
    year = re.search(r'\d{4}', adate)
    month_date = re.search(r'\d{2}-\d{2}', adate)
    new_date = year[0] + "-" + month_date[0]
    os.makedirs(os.path.join("financial", new_date), exist_ok=True)

1 Answer

Your code doesn't work when you pass in a date with year first ('2018-11-18') because the month_date will be '18-11', the first occurrence of 2 digits followed by a hyphen and 2 digits. Try using string slice syntax instead of regex; it will be easier. (The challenge doesn't import re so it means you can do it without using regex.)