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
Sometimes our files or directories aren't named correctly. Python provides us with a few functions for handling the changing of file and directory locations.
Python Documentation
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
Sometimes we find ourselves
needing to just move a file or
0:00
rename an existing file.
0:03
That's the work that needs to be done.
0:05
I used to have a process for deploying new
versions of a site that was mostly built
0:06
about moving and renaming directories,
before restarting the server process,
0:10
so that it would find
the new version of a site.
0:14
Python provides us with a few functions
for handling the changing of file and
0:17
directory locations.
0:20
We're gonna look at two of these first.
0:21
Os.rename, and it's intermediate
friendly friend, os.renames.
0:23
With rename,
we can change the name of one item.
0:29
So we can say os.rename, and
I'm gonna rename bootstrap to assets.
0:32
And if I look in my directory over here,
I can see I have assets, and
0:38
I don't have bootstrap anymore.
0:42
But if we try to do that with a file and
give it a directory for
0:44
the, to, location,
sometimes we'll get an error.
0:47
So let's try doing that to this tree.py.
0:51
And we want to move that to,
say, scripts/python.
0:53
And we go back to this FileNotFoundError.
1:01
Sometimes you may find
a NotADirectoryError as well,
1:04
one of those two.
1:08
And we know it's directory because we
have this trailing slash here on the end.
1:08
So thats why you may get
the NotADirectoryError.
1:12
If don't necessarily have all the
intermediate steps for the new location,
1:15
os.renames comes to the rescue, which,
admittedly, is kind of a weird name.
1:18
But let's do os.renames.
1:22
And I wanna go from assets to,
say, static/raw.
1:27
So static doesn't exist, and neither does
raw, but I wanna move assets to there.
1:33
So if I come over here and I look,
I have my static directory, I have my raw
1:36
directory, and that contains all of
the stuff that bootstrap used to contain.
1:41
This created the new static directory, and
1:46
it renamed the old assets directory as,
raw, and put it inside of there.
1:48
So that's pretty handy.
1:53
The os.rename function can have
a few cross-platform issues.
1:55
You can find out exactly what those are in
the docs, but, to help avoid these,
1:59
you can use os.replace instead.
2:03
So let's do os.replace, and
we wanna replace static/raw with assets.
2:04
Actually, let's go back to bootstrap.
2:11
And now, if I look here,
I have my bootstrap directory back, and
2:15
it still has all of its stuff in it.
2:18
And the static directory has
nothing inside of there.
2:21
So it didn't remove static, but
it did move raw back outside of static.
2:24
So I'm gonna go ahead and delete that.
2:29
Os.replace won't handle cases where
the intermediate directories don't
2:31
exist, though.
2:34
You'd have to create them first with
os.makedrs, or use os.renames instead.
2:35
Admittedly, Python's options for moving
files and directories are a little sparse.
2:42
But that's all right.
2:45
I found, most of the time, you're more
concerned with creating new files and
2:46
directories and
cleaning up ones you no longer need.
2:49
We'll look into that,
more, in the next video.
2:51
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