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
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
We need to find the directory of where our project is being built.
This video doesn't have any notes.
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
I'm gonna build this whole project
in a file named builder.py.
0:00
To use this tool, we'll just run
the script at the command line and
0:04
give it some data.
0:07
So let's start off with def main, and
we'll say that this is our Entrypoint.
0:08
And then down here, we'll do our
normal if __name__ == __main__.
0:14
And then we'll run main.
0:21
We'll start with just a single entry point
which we'll run when the script is run
0:22
directly.
0:27
So first, I wanna get the path for
the project.
0:28
Now, since I need to make sure that
the user gives me something like a path,
0:31
it should really be an absolute path.
0:34
I think I wanna bring in pathlib.
0:35
I also think I'll do
this in a new function.
0:38
So let's first go up here and
do import pathlib.
0:40
And then let's write a new
function called get_root.
0:44
And we're gonna say root
= pathlib.PurePath.
0:47
And then we'll say input,
0:51
what's the full path where
you'd like the project?
0:54
Let's come down here, and
we'll say if not root.is_absolute,
1:00
then we'll return get_root.
1:06
Otherwise, we're going to return
the root that they gave us.
1:10
Nothing's too tricky here.
1:15
One thing you might not have seen before,
though,
1:15
is this way of returning
a new call to the function.
1:18
This is an easy way to just
reset the function and
1:22
make the user do what you want them to do.
1:24
Okay, so let's go ahead and
assign that variable here in main.
1:26
We'll say project_root = get_root.
1:32
Now, we could test this part, but let's
add one more thing first, a project name.
1:36
Same idea, but I think I'll do
this one directly here in main,
1:40
since it doesn't have
a whole lot of ways to fail.
1:43
So I'll say project_name = None.
1:46
And while there's not a project_name,
1:50
project_name = input("What's
the full name for
1:53
the project?") and
I want to call strip() on the end of that
1:57
to make sure that there is no spaces or
anything like that.
2:02
All right, so
I think now we should test it.
2:07
Let's add just a little bit right here.
2:11
We'll just say,
print("Creating {} in {}") and
2:12
then we'll format that with
project_name and project_root.
2:17
All right, so
now we should be able to run this.
2:23
So let's try doing python3 builder.py.
2:29
What's the full path where
we'd like this project?
2:33
Projects and
then file_systems and then demo.
2:35
It doesn't like that.
2:40
/Users/Kenneth/Projects/file_systems/de-
mo/.
2:43
And the name for
this will be Demo Project.
2:50
So it's gonna create a demo project in
/Users/Kenneth/Projects/file_systems/demo.
2:53
So, cool.
2:59
If I look here, just to make sure
I have the right path, I do.
3:00
/Users/Kenneth/Projects/file_systems
great.
3:03
So that's going to create that
in the path that I would like.
3:07
Now, if we give it a bad
path like one from Windows,
3:11
since I'm currently on a POSIX system,
I should get asked for a new one.
3:13
And if I give it a good path and a project
name like we said, we'll get both back.
3:16
So awesome.
3:21
And just like that, we're on our
way to making a project generator.
3:23
Something you want might add is asking for
3:26
a user's full name and
maybe a license choice.
3:28
I don't think I'm going to include
either of those, but they'd make for
3:30
a more fully featured app.
3:33
In the next video, we'll start
creating the directories we'll need.
3:34
We need to make sure we're not
stomping on any existing files,
3:37
at least not without a warning.
3:39
And we'll need to get some sort of file
system safe name for the project too.
3:41
Try and do those on your own if you want,
then come back for my approach.
3:43
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