Accessing Files and Folders (Review Exercises)
00:00 Time for a couple of review exercises before you move on to the next steps.
Start by creating a new
Path object that points to a file called
my_file.txt and place it in a folder,
my_folder/ , and then put it into your computer’s home directory.
Then assign this
Path object to the variable name
file_path. Once you have that, check whether the path assigned to
file_path exists, then print the name of the path assigned to
The output should be a string that says
'my_file.txt'. Finally, print the name of the parent directory of the path assigned to
The output here should be, again, a string,
'my_folder'. As a bonus, you can also see if you remember how you can pick apart the filename
my_file.txt and just print out the stem and the file suffix separately.
Do these exercises locally on your computer to get a bit of practice using
pathlib and to solidify the different methods and attributes on
Path objects that you’ve heard about in this section of the course.
Become a Member to join the conversation.