Accessing Files and Folders (Review Exercises)
00:00 Time for a couple of review exercises before you move on to the next steps.
00:05
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.
00:16
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 file_path
.
00:31
The output should be a string that says 'my_file.txt'
. Finally, print the name of the parent directory of the path assigned to file_path
.
00:42
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.
00:58
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.
Martin Breuss RP Team on Jan. 3, 2024
@brianmcquaig hm, my first intuition was to say that it should keep saying path—as it currently.
Probably because I want to highlight that the Path
object is only that—a Path
object—and not a file.
But I see that the task is just to print the file name part of the path, so maybe a better way to describe this would be:
Print the file name of the path assigned to
file_path
. The output should be'my_file.txt'
.
What do you think about that? Does that make it more clear?
Become a Member to join the conversation.
brianmcquaig on Dec. 24, 2023
Shouldn’t exercise #3 read (change in caps)?:
Print the name of the FILE assigned to file_path. The output should be ‘my_file.txt’.