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

Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

quiz help: passing various date formats to return a file path described by a date format

My first questions is... can I not plug a method like re.match into an if statement, and if matched return something truthy??? I feel like I may have never fully grasped the truthy concept and I feel like I really need to understand it with this problem because I first tried an if statement asking if re.match == date. But a match object cannot be compared to the argument passed in. Or can it??

Im testing this code by passing the argument '2000-01-01' it starts going to the else block but i feel like it should never get that far.

My second question is.. am I making this way harder than it really is supposed to be? Is there a clue that you might be able to give me without giving me the answer?

And finally... What is the answer?

backup.py
import os
import re
import datetime

def create_daily_dir(date):
    if re.match('\d\d\d\d-\d\d-\d\d', date) == True:
        return os.makedirs(f'financial/{date}')
    else:
        dt = datetime.datetime.strptime(date, '%d-%m-%Y')
        return os.makedirs(f'financial/{dt.strftime("%Y-%m-%d")}')


create_daily_dir('2000-01-01')

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Hey Andy McDonald, you are on the right track!

You absolutely can use a re.match object in a "truthy" fashion. Comparing to True means the other object must evaluate to a bool true or false. In a truthy context, the object needs to be non-False. Since a successful match object is non-false, it is itself "truthy". :point_right: drop the == True portion.

You have a second error. The strptime string has the month and day reversed. should be month before day instead of day before month

You approach is valid. Another path is to regex or split to break out the portions (with the trick being the year has to be first or last).

Post back if you have any more questions. Good luck!