00:00 In this lesson, I’m going to cover how to make directories and even entire directory trees.
As usual, there are a few different ways of doing this. With the
os module, if you want to create just a single directory, you can use
os.mkdir() (make directory) with the directory name, which creates a single subdirectory with the given name.
Important to note is that this will not create a directory and subdirectories. If you need that, you need to use
os.makedirs() and then you can pass in a full
path_name with multiple subdirectories and it will create that full directory tree, as needed.
pathlib module, you can use
pathlib.Path.mkdir(), which functions in essentially the same way as
os.mkdir(), in that it will not create full directory trees, either.
00:48 In this lesson, I’m not going to use, really, any specific sample directory, because I’m just going to be creating directories as I need them. Let’s move right on over into the REPL.
Okay, let’s make some directories!
os.mkdir() is the first and the most basic way to do this. As you can see, it takes in a
path parameter, and then it can also take in a
mode parameter, and then
01:13 I won’t get into, really, how these work directly, just know that this has to do with file permissions, and then what to do if you have symbolic links or something else like that in your directory.
So, if I want to create a directory, just called
test, that’s pretty simple to do, and then I can use
listdir() to see that it’s been created.
Now, if I wanted to do something a little bit more specific, and I wanted to say something like
then you’ll see that that actually doesn’t work because
test2/ doesn’t yet exist, so
sub_test/ can’t be created by
If I need to do something like that I can use
os.makedirs() which will make this directory, and then it will make all of these, it will just make everything that I need in order to get this final directory.
So as you can see, I
listdir() and I have
'test2' in there. Then if I call
then you can see that it has the
'sub_test' folder, just as I needed. So, that’s really pretty simple and awesome. The reason that
mkdir() exists in this way, and the reason that it has this behavior is just to really be, kind of, a safety valve, a safety mechanism, to make sure that you really know exactly what’s going on with your filesystem at all times.
makedirs() is awesome, but it can also cause unexpected behavior if you think you’re in one directory and you make a whole chain of subdirectories, and then you end up in a totally unexpected place, whereas
mkdir() is built to, kind of, error out before that happens.
02:45 So, that’s how you can handle this. Something else to note here is that both of these will throw an exception if you try to create a directory that exists.
If I say
os.makedirs("test"), then Python will throw me a
FileExistsError, because it’s trying to create something that already exists.
pathlib.Path() alternative, which can be accomplished this way, you can avoid that problem if you’re trying to do that. So, you can say something like this: a
new_path = Path("test").
And then you can say
new_path.mkdir(), and you can use the
exist_ok parameter and set that to
True if you don’t want it to throw an error if it already exists.
03:30 Now, the issue with this, of course, is that this just ends up doing nothing because it doesn’t make a directory, because it already exists. It just doesn’t tell you that loudly, it just, kind of, silently suppresses that error.
03:42 That’s definitely something that’s important to note, is that even if you don’t want that error to happen, you probably still wanted to know that something is happening there because otherwise, you could run into problems where you think you’re creating something new, but really you’re trying to create something, and there’s already an existing directory.
03:59 So, this behavior, you can eliminate if you need it, but you might not want to at all times. So, those are the different ways to create directories in Python, and I think it’s really pretty simple and straightforward.
pathlib.mkdir() if you want to use the more object-oriented framework, and you only need to create one directory at a time. Use
And then if you need to create a full directory tree, with subdirectories, use
os.makedirs(), or you can use the
parents parameter of the
Just know that that’s not the default behavior of the
04:39 In the next lesson, I’ll tell you how to delete all of these things that you’ve now learned how to make.
Become a Member to join the conversation.