Making Directories
00:00 In this lesson, I’m going to cover how to make directories and even entire directory trees.
00:07
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.
00:20
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.
00:36
With the 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.
00:59
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 dir_fd
parameter.
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.
01:24
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.
01:32
Now, if I wanted to do something a little bit more specific, and I wanted to say something like os.mkdir("test2/sub_test")
,
01:43
then you’ll see that that actually doesn’t work because test2/
doesn’t yet exist, so sub_test/
can’t be created by mkdir()
.
01:50
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.
02:04
So as you can see, I listdir()
and I have 'test2'
in there. Then if I call listdir()
on "test2"
,
02:12
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.
02:30
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.
02:56
If I say os.mkdir("test")
or os.makedirs("test")
, then Python will throw me a FileExistsError
, because it’s trying to create something that already exists.
03:07
The 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")
.
03:20
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.
04:11
Use pathlib.mkdir()
if you want to use the more object-oriented framework, and you only need to create one directory at a time. Use os.mkdir()
, otherwise.
04:21
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 pathlib.Path.mkdir()
option.
04:33
Just know that that’s not the default behavior of the pathlib.Path()
version.
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.