Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Creating a Subdirectory

00:00 So you’ve created a new notes directory, but in here you want to keep track of plans on a different schedule. Let’s say you have monthly plans and weekly plans.

00:10 So you want to create a subdirectory to this notes directory that is inside of yet another subdirectory called plans. Let me type this out. It will be a little easier to understand.

00:21 So, say you want a monthly_dir and it should be inside of your notes_dir and then inside of a "plans" directory, and then it should be called "monthly".

00:36 This is where you’re going to store your monthly plans, let’s say. Now you can create this path just like this.

00:44 No complaints. The path shows up beautifully. But now if you want to create this directory, you’ll run into an error. monthly_dir.mkdir() gives you a FileNotFoundError because, while the notes directory exists, you did not yet create the plans directory. In general, to be able to create a directory, all the parent directories need to already exist. However, there’s a different way of doing it by setting another parameter to True.

01:16 It goes like this. If you say monthly_dir.mkdir() and then pass parents the value True,

01:28 then you see that Python doesn’t complain, and monthly_dir does now exist. So all the intermediate subfolders—in this case, it’s only one, called "plans"—get created if you pass parents=True to .mkdir(). So putting these two things together, the one you saw before about exist_ok, and parents gives you a common pattern when creating directories using Python’s pathlib module. Let’s make a new directory.

02:00 So aside from your monthly plans, you also want to have weekly plans. So weekly_dir

02:14 "plans" and then "weekly".

02:17 And even if the subfolder "plans" wouldn’t exist, now you could create everything you need like this: saying weekly_dir.mkdir() and give it parents=True and exist_ok also True.

02:37 This is a common pattern, where you set both the .parents attribute as well as the .exist_ok attribute to True, but it might not make sense in all situations.

02:46 For example, if you want a user to pass a certain path, you might want to double check that the path already exists and that they’re not accidentally mistyping the path that they wanna save a file to, for example, just by adding an accidental typo. But you’ll commonly see, and probably also commonly use, this pattern of using parents=True and exist_ok also True. All right, enough with folders. In the next lesson, you’ll learn how to create files using pathlib.

Become a Member to join the conversation.