Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's explore how to create files and directory trees in Python.
Treehouse Course
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Welcome back, we've been looking
at the tools Python provides for
0:04
making our way around file systems.
0:07
We're now able to change the directory
we're working in, create paths for
0:09
the directories and files, and
0:12
check the stats of a file to see
how big it is and other things.
0:13
While all of that's pretty handy,
none of that gets us very far if we can't
0:17
do things with the files and
directories that we find.
0:20
This stage won't focus too
much on writing to files.
0:22
Check the teacher's notes if you want
more information on working with files.
0:25
Instead, we're going to look at
Python's tools for creating files and
0:28
directories as a step by itself.
0:31
While this will include writing to files,
we won't spend a lot of time on that.
0:33
Okay, let's go see how we can use Python
to create our files and directory trees.
0:37
Before we try to make a file or
0:41
directory, we should probably make sure
that it doesn't already exist, right?
0:43
The OS dot path module already
has a method for this.
0:47
So let's say I wanted to make the
bootstrap directory in this workspace so
0:50
I've imported the OS,
I can do OS dot path dot exists.
0:54
And I can search for bootstrap, and
I get back true, that it does exist so
0:58
great, I know it exists,
I can avoid trying to create it again.
1:04
And this works for files as well so
I can do bootstrap/js/npm.js.
1:07
And I get back True.
1:15
If I search for say,
treehouse.js, I get back False,
1:16
because that one does not exist.
1:20
So, that's cool.
1:22
I can know whether or
not a file or directory exists.
1:23
And, I can create any that I'm missing.
1:27
For creating files,
I can of course just use open.
1:30
And I can say that I'm going to open,
like test_file.txt.
1:34
And I can use W,
to truncate it or erase it, and
1:38
then start writing to it, or I can use A,
to append to whatever's there.
1:42
And then I can just close the file.
1:46
File's been opened,
it's been written to, it's been closed.
1:49
And that works, but
it takes two steps, and
1:51
I have to remember to use either W or A.
1:53
If I was to try to do, like say,
test_file2 and I left off the w.
1:56
Then Python will complain at me
that that file doesn't exist.
2:01
So to avoid doing that,
I would love to use the os.mknod method.
2:05
But I can't because I'm
showing this to you on a Mac.
2:13
So normally here I would do
bootstrap/js/treehouse.js and
2:16
then it would create it.
2:20
But if I try and
do this I get back permission denied.
2:23
And the reason is because on MacOS and
MacOS only, for
2:26
Python to use the os.mknod [SP] function
you have to be running Python as root,
2:31
so you would have to have
done like sudo python three.
2:37
Now, I don't like that.
2:41
I don't like that you have to do that, but
2:43
I can't change that because I have
no control over what Apple does.
2:44
So if you're watching this course and
2:47
you're on a Mac, don't bother with
OS.mknod, you'll have to use the open and
2:49
close method, or one of various other
methods that you can find by Googling.
2:54
If you're on Linux or Windows,
the OS.mknod will work just fine.
3:00
You can create the files.
3:05
And by default it will create a file
with the permissions set to 0600.
3:06
If you need to know more about modes
you can check the teacher's notes for
3:11
our console foundations course.
3:15
MKnod can be used to make
more than just files but
3:17
that's way beyond
the scope of this course.
3:19
As always, check the documentation for
more information and examples.
3:22
Okay, so, small rant and
mknod aside, what about directories?
3:26
Well, we have two options for
making directories in Python.
3:32
If I only want to make one directory
I can do that directly with os.mkdir.
3:35
So I could just say,
make the directory templates and
3:40
Python will make this directory.
3:44
If I look over here, I can see hey,
I have a templates directory and
3:47
there's my test file that I created and
didn't do anything with.
3:50
Using mkdir though would get very annoying
if you had to make a deep tree of
3:55
directories that all had to be
made from the bottom back up.
3:59
Or rather, from the top down because you'd
have to do it one directory at a time.
4:03
Luckily there is the makedirs function.
4:08
And this one will create
them all the way down.
4:12
First though,
I wanna delete this templates directory so
4:15
I'm gonna delete that, and then I'm gonna
make, templates, layouts, and mobile.
4:18
So you can see, I have three directories
there, the templates directory and
4:24
inside of that it's gonna be
the layouts directory, and
4:27
inside of that will be
the mobile directory.
4:29
So now if I come over and I check,
I have templates, I have layouts and
4:32
I have mobile.
4:36
And on the inside of mobile,
4:37
there's nothing because I
haven't put anything there.
4:38
Makedirs has an extra argument though
that I think you should know about.
4:41
Let's try running the command again,
exactly as we just ran it.
4:45
And you can see we get back this new
error which is the FileExistsError.
4:48
And it says that this
directory already exists.
4:53
It's kind of funny that
it's called FileExists
4:55
when it's a directory, but that's fine.
4:57
If the target directory,
4:59
the one at the end, already exists,
you're gonna get this error.
5:00
If that matters to you, great.
5:04
Catch the error,
do whatever you need to do there.
5:05
Tell the user that the directory
already exists and
5:08
you can't make a new one or whatever.
5:10
If it doesn't though, if it doesn't
matter to you that the directory exists,
5:12
you can tell Python that it's
okay if the directory exists.
5:16
So let me clear the screen here and
we'll run this again.
5:20
And this time we will say, exist_ok=True
and Python doesn't complain at us.
5:23
Python goes you don't care
if the directory exists?
5:29
The directory exists,
we're just gonna move on.
5:32
I've found that most of the time,
this is the behavior that I actually want.
5:34
There are two things on every function
we've used that's handy to know.
5:38
First, all of these paths we've
been providing are relative, but
5:43
you can provide absolute paths, too.
5:45
The functions will still
work the same way.
5:47
They'll just go to the absolute
location you provided,
5:48
instead of going forward from
the current working directory.
5:51
Second, you can create a path-like object,
like you'd get from pathlib, and
5:54
provide that instead of
providing a path as a string.
5:58
It's often much handier when working with
paths that are generated by your software
6:01
or from user input instead of paths that
you're typing directly into your code.
6:04
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up