Editor

Change File Extension

Intermediate · Write the code · from Python pathlib: Modern File Path Handling

From the lesson: Python pathlib: Modern File Path Handling

Imagine navigating to a friend's house. You could follow vague paper directions like "turn left at the big tree, then right at the red mailbox." Or you could use GPS, which understands addresses, calculates routes, and handles all the tricky details for you.

Read the lesson

Given the path /data/report.csv, use pathlib to create a new path with the extension changed to .json. Print both the original and new path.

Expected output:

Original: /data/report.csv
Converted: /data/report.json
from pathlib import PurePosixPath
original = PurePosixPath('/data/report.csv')
# Your code here

More pathlib exercises

All Python exercises · 50 practice problems · File I/O & Data Formats quiz