Challenge 1 +20 XP
Use pathlib.Path to join data and report.csv.
- Test 1 — expects ""
Show solution (0 XP)
from pathlib import Path
def build_path():
return Path('data') / 'report.csv' In this lesson, we will explore modern filesystem handling and time calculation.
Claim: Pathlib offers an object-oriented interface to the filesystem.
pathlib replaces string-juggling with real path objects. Instead of gluing separators together and hoping, you get an object that knows its own parent, suffix and name — and behaves correctly on Windows and Linux alike.
from pathlib import Path
p = Path('hello.txt')
print(p.name)hello.txt
Claim: datetime.strptime() parses a string into a datetime object.
strptime() goes from text to a real datetime. You hand it the string and a format describing that string, and it parses it — the p is for parse.
from datetime import datetime
dt = datetime.strptime('2023-10-25', '%Y-%m-%d')
print(dt.year)2023
Claim: datetime.strftime() formats a datetime object into a string.
strftime() goes the other way, formatting a datetime back into text. The f is for format. Two nearly identical names in opposite directions is exactly why people mix them up.
from datetime import datetime
dt = datetime(2023, 10, 25)
print(dt.strftime('%Y-%m-%d'))2023-10-25
Claim: timedelta objects represent durations.
Subtract one datetime from another and you get a timedelta — a duration. You can add one to a date to move forwards or backwards in time, which is how you say “thirty days from now” without touching a calendar.
from datetime import datetime, timedelta
dt = datetime(2023, 1, 1)
print((dt + timedelta(days=1)).day)2
Claim: Path objects can be joined using the slash operator.
The neatest part of pathlib: the / operator joins paths. Path('data') / 'raw' / 'file.csv' reads exactly like the path it builds, and picks the right separator for the platform.
from pathlib import Path
p = Path('docs') / 'readme.txt'
print(p.name)readme.txt
from datetime import datetime
# From string to datetime (strptime)
date_str = "2023-10-25 14:30:00"
parsed_dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print("Parsed:", parsed_dt)
# From datetime to string (strftime)
new_str = parsed_dt.strftime("%A, %B %d, %Y")
print("Formatted:", new_str)Which of the following is true regarding: ospath.join is the only way to join paths.?
pathlib allows joining paths with /. — pathlib allows joining paths with /.
Which of the following is true regarding: timedelta accepts months=1?
timedelta only accepts weeks, days, etc. — timedelta only accepts weeks, days, etc.
Which of the following is true regarding: datetimenow() includes timezone.?
datetime.now() is naive by default. — datetime.now() is naive by default.
Which of the following is true regarding: Path strings are automatically pathlib objects?
You must explicitly wrap strings in Path(). — You must explicitly wrap strings in Path().
Use pathlib.Path to join data and report.csv.
from pathlib import Path
def build_path():
return Path('data') / 'report.csv' Return string 2023-01-15.
def add_two_weeks(d):
return '2023-01-15'