Time
2 hours
Difficulty
Intermediate
CEU/CPE
2

Video Transcription

00:00
Hello and welcome back to Intermediate Python here on Cyber Eri on demand. I is always in your instructor, Joe Perry, and I'm thrilled to have you here in less than three interfacing with the operating system in this lesson, we're gonna be talking about three core concepts. We're gonna understand the OS module, what it is, what it does, what it's for.
00:18
We're gonna learn how we can examine files programmatically and we're going to use that kind of touch on a couple of advanced concepts will be looking at in later modules.
00:25
We're gonna learn how we can manipulate the system directly from Python.
00:29
So to do that, we're gonna work out of our trusty handy dandy VM.
00:33
And we're gonna first start out just by opening our interpreter because we want to get a look at this module and we've not spent a ton of time on modules yet because we've got a whole module relating to those who were gonna do deep dives but won't understand this particular module and see what it's for. So the easiest way to do that is first, by importing OS and then just doing a simple help command,
00:52
you can see here
00:54
it's O s routines for the ante or post six, depending on what system were on More Simply put, this is a This is a methodology or a mechanism by which the Python interpreter is capable of interacting with the operating system directly.
01:07
And as we scroll down, we can see it exports a bunch of different functions that are built into that operating system.
01:14
Unlinked, stat, has pwd changed her? That sort of thing OS path is for use is for finding actual paths. And working with those name is either posing serenity.
01:26
So there are a bunch of different functions here that are exported from O. S that are just designed to interact directly with your operating system. And it's definitely worth taking some time to look through it in the python manuals
01:37
and get a sense of the different things you can do with it. But for now, for this video, what we really want to do, what really want to focus on is one specific function, and the reason we're gonna focus on that one specific function is because it's our second objective. The examined files programmatically or actually gonna be doing here is we're going to list all of the files
01:56
on this operating system. We're going to do it by making use of the function OS stock.
02:00
Now, as I said, talking about us not walk, we're actually gonna touch on a couple of concepts we haven't seen yet. That's okay. This is intended to introduce those eso. When we look at help, we can see that it takes a bunch of different arguments, some of which have this equal sign these air called keyword arguments. We haven't really dug into those a lot yet, and we're totally going to
02:20
We're gonna spend a little bit more time on the one we're talking about modules and more advanced sort of Pilon functionality.
02:24
For now, what we need to know is there's one argument that it simply must have in order to function, which is the top argument. And the top argument you can see is the root of the directory treat that it's going to be examining. And it's worth noting. It says every directory in the directory tree, excluding dot and dot dot
02:42
These these two are special pieces of functionality built into the operating system.
02:46
This is a self reference and this is a parental reference. So this, this is saying that is not going to search its own directory and create a feedback loop so they're not gonna search the parent directory and create a feedback loop that way,
02:58
it yields a three to pull those air two concepts that we have not touched upon yet. We're gonna talk about a tuba here in just a second. Yields is gonna be part of our generators and generators lessons. So don't worry too much about what that is or how it works just yet. So two people is actually a pretty simple data structure that simply hasn't come up because we haven't been performing the kind of programming
03:17
and either the intro class or so far in the intermediate class
03:20
that makes useful. But now that we are, it's worth seeing what, how they are defined. What, therefore, and to pull is actually a very simple data structure that is defined similarly to a list with a series of items. And it's enclosed with thes parentheses instead of brackets. And you can see here that run tight on to one.
03:38
Okay,
03:38
it's a to pull. Now
03:40
you can address it to pull the same way you do with a list. You can address it via indexing.
03:46
It has a lot of
03:49
commands or attributes in common with lists, as you can see things like Add Town Index. All of those there are in common with lists, but there are some decisions under the hood that are a little bit more advanced. We're going to get into even in this course, this intermediate course in general.
04:03
In the advanced course. We do spend some time. We will spend some time on really deep diving into these data types,
04:10
much more so than we've done in this course of the previous course, where we're examining sort of under the hood
04:15
in sort of an abstract, theoretical way, how these data types were constructed.
04:19
For now. What we need to know is that we can address the trouble like a list, and it's usable in many of the same situations, and that's gonna be more than enough for us to be going forward on.
04:29
So we said that OS not walk yields Ah, 32 and we're not gonna worry about what yielding is other than to understand that we can address it for example, as four
04:40
item one Item two,
04:44
Item three
04:46
in O s Stop Walk.
04:49
And we're gonna give it a simple single argument, which is gonna be our current working directory, which I actually think I still have saved on my clipboard
04:58
from a previous take. Yep.
05:01
Sorry to spoil the movie magic for anyone watching, but these videos take a couple dozen, takes a piece, there's a There's a lot that goes into him. So every once in a while you might see an artifact from where I slice footage in. So I hope that doesn't ruin it too much for you. But so for item 12 and three in os dot walks, remember that this is yielding a three to pull,
05:20
which we saw are the directory that we're examining
05:23
all subdirectories and all sub files.
05:27
It's going to perform some operation in this case. We're gonna use this for Luke. Just two prints,
05:32
all of those items.
05:42
Format item one,
05:45
Item two,
05:47
Item three.
05:50
And let's just see what this gives us.
05:55
You can see that it kind of printed out pretty ugly looking, but it printed out the current working directory that are Colin here and then it printed out lesson One lesson to which are the two subdirectories we have and all of the files, which is just the one junk dot dat file that we have here in this in this directory.
06:13
Additionally, the next thing that it did was drop down into a lesson one here where it printed no subdirectories because there are none. And it printed command line dot pie an interpreter, not pie, because those were those were the files we created in that lessen the next line down it created again. We see that there are no subdirectories toe work with,
06:31
but there are plenty of sub there plenty of files inside that directory.
06:35
So
06:36
this is kind of ugly looking because I made this list very simply and very quickly. But the idea that we're working with here is that what we were able to do is just from our host director for top directory. Were able to see all of the sub directories in this first list right here,
06:48
and we were able to then walk into them using this OS out walk. It did all of the work force of finding those. Now we could certainly go through this in pretty up the language a little bit. And in fact, in one of the assignments you'll be working on, it's going to be creating a much more robust a version of this technique in which you're going to actually attempt to
07:05
list every single item on the operating system
07:10
individually with it with a clear Absolut path. But for now, all I wanted to show us how the OS walk function works so that you understand how you can use this to actually interact with files and directories on the machine that's done. We're gonna look at one last piece of functionality in OS, and that's one of my favorite pieces. And it's something that I use really really often
07:29
because it's sort of a shorthand for addressing items on the system,
07:32
and that is os dot system, and again, we're just gonna do a quick help on that to see what it's for.
07:40
We could see that it is a very, very simple function. You give it a single arguing, which is the command, and it executes the command in a sub show. So, for example, a less that system
07:49
clear
07:51
clears the screen as I do pathologically now control. L works a little bit better in the Python interpreter, but it's still there. This pike's back the results to your interpreter as you're working so you can see it less that system. PWD is going to give you back that answer
08:07
on, and that's really useful to do so. You can perform all sorts of system activities, could manipulate the system you can do. For example, CD
08:16
changed the parent directory and then you contest and see where you are. With PWD,
08:20
you can see that you're able to operate all of these different sub shell changes, but they aren't actually interfering with one another. And that's that's what we're addressing right here when I showed you so we changed to the parent directory. But then when we did a current director, our president working directory, we saw that was still in the same one. And that's something that's worth addressing because that sub show
08:41
is only going to be executed for as long as that single command takes.
08:46
So when you're working with a West system, you need to remember that you're kind of working without context there. It's only going to execute that one commanded a time, and there are more robust ways to implement it. But this is, you know, a 6 to 9 minute video on. I wanted you to be aware of the system functionality so that you can take that kind of go forth and play around with it.
09:03
So that is gonna be the end of this video. This is gonna be the end of lesson three and therefore the end of the first module. The next video, see, is gonna be a summary and review what we talked about all the different concepts that we touched on. And, of course, as I'm sure you're aware by now, by watching these python class is the best thing you can do now, instead of jumping straight into Module two,
09:20
is to take some time off and play with the tools that have been introduced.
09:24
These videos are intentionally, short and intentionally just kind of brush upon the information that you need and give you the tools you need to start playing around with it. Because the single best way to learn python the single best way to learn any programming language is to actually write the code. So that's what you're going to do now. That's what I highly recommend you do now. We have some lab assignments you can look at.
09:43
You can do the beginning part of our
09:45
lab with the lad that was provided for us by code of all. Or by next check. They used to be called Code of all. Ah, highly recommend you do that. And then once you feel comfortable with the concept of disgust, you come back and have a look at module to where we're gonna have a ton of fun. And we're going to be talking about all of the different concepts of classes in
10:03
Python. So thank you all for watching.
10:05
Thank you for watching Intermediate Python here on Cyber A on demand, As always, I've been your instructor, Joe Perry, and I can't wait to see you back in our next video.

Up Next

Intermediate Python

This Intermediate Python course uses and builds upon the materials from Intro to Python, using the Python language to accomplish more complex and difficult tasks from building Modules from scratch to constructing and using custom Classes. You’ll learn to make the most of Python’s object orientation, and set yourself up for advanced applications.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor