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 trialjdee
6,655 Pointshelp
Hi,
My code is not being accepted though it works with the below calls in Eclipse using pydev. Also, I tried pulling up a workspace for this to get better traceroutes, but when I create a new one, then pick a port upon prompt, it says it's unavailable. I have a ticket in with Support.
create_daily_dir("03-27-2018") create_daily_dir("2014-12-21")
import os
import re
def create_daily_dir(string):
if (os.path.exists('financial')):
### Test for date pattern
# yyyy-mm-dd
if( re.match(r'\d{4}-\d{2}-\d{2}', string)):
# Correct pattern
os.mkdir("financial\\" + string)
# mm-dd-yyyy
elif( re.match(r'\d{2}-\d{2}-\d{4}', string)):
# Incorrect pattern
mm, dd, yyyy = string.split("-")
os.mkdir("financial\\" + yyyy+"-"+mm+"-"+dd)
else:
print("Incorrect input!")
3 Answers
KRIS NIKOLAISEN
54,972 PointsYou should be using a forward slash in your path. The instructions give an example:
we'd have a directory structure like financial/2017-04-22/
If you want to try this in a workspace paste the following into a file named test.py.
import os
import re
def create_daily_dir(string):
if (os.path.exists('financial')):
### Test for date pattern
# yyyy-mm-dd
if( re.match(r'\d{4}-\d{2}-\d{2}', string)):
# Correct pattern
os.mkdir("financial/" + string)
# mm-dd-yyyy
elif( re.match(r'\d{2}-\d{2}-\d{4}', string)):
# Incorrect pattern
mm, dd, yyyy = string.split("-")
os.mkdir("financial/" + yyyy+"-"+mm+"-"+dd)
else:
print("Incorrect input!")
os.mkdir('financial')
create_daily_dir("10-09-2019")
create_daily_dir("2019-10-10")
Then in the console enter python test.py
. From here you can right click the left hand pane with the workspace file list and choose Refresh. Or use console command ls
to list files and folders and see the new financial directory; or cd financial
to change to the financial directory then ls
to list its contents.
jdee
6,655 PointsThanks. You're suggestion worked in the workspace.
One last question. Below is what I submitted and it worked - using os.mkdir("financial/" + string) if date string is correct, but os.path.join(os.getcwd(), yyyy+"-"+mm+"-"+dd) if date string not correct.
It would not accept it with os.path.join(os.getcwd(), string) if date is correct.
Why?
def create_daily_dir(string):
### date pattern filter
# yyyy-mm-dd
if( re.match(r'\d{4}-\d{2}-\d{2}', string)):
# Correct pattern
os.mkdir("financial/" + string) # <= this worked here...but why
#os.path.join(os.getcwd(), string)
# mm-dd-yyyy
elif( re.match(r'\d{2}-\d{2}-\d{4}', string)):
# Incorrect pattern
mm, dd, yyyy = string.split("-")
#os.mkdir("financial/" + yyyy+"-"+mm+"-"+dd)
os.path.join(os.getcwd(), yyyy+"-"+mm+"-"+dd)
else:
print("Incorrect input!")
KRIS NIKOLAISEN
54,972 PointsWas kind of confused at first because the I re-read the instructions and they state financial
is the current directory. I read the docs and it looks like relative directories are supported.
os.path.join() returns the path as a string so you'd have to pass that to os.mkdir() to create the directory. I tried in a workspace where I modified test.py and moved it to the financial directory and got the following to work.
import os
import re
def create_daily_dir(string):
### Test for date pattern
# yyyy-mm-dd
if( re.match(r'\d{4}-\d{2}-\d{2}', string)):
# Correct pattern
print("making " + os.path.join(os.getcwd(), string))
os.mkdir(os.path.join(os.getcwd(), string))
# mm-dd-yyyy
elif( re.match(r'\d{2}-\d{2}-\d{4}', string)):
# Incorrect pattern
mm, dd, yyyy = string.split("-")
print("making yyyy-mm-dd")
os.mkdir(yyyy+"-"+mm+"-"+dd)
else:
print("Incorrect input!")
create_daily_dir("10-09-2019")
create_daily_dir("2019-10-10")
Can't get it to work in the challenge though. I receive Bummer: Didn't find the right directory for a month-day-year date.