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.
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.
We're gonna learn how we can manipulate the system directly from Python.
So to do that, we're gonna work out of our trusty handy dandy VM.
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,
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.
And as we scroll down, we can see it exports a bunch of different functions that are built into that operating system.
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.
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
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
on this operating system. We're going to do it by making use of the function OS stock.
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
We're gonna spend a little bit more time on the one we're talking about modules and more advanced sort of Pilon functionality.
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
These these two are special pieces of functionality built into the operating system.
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,
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
and either the intro class or so far in the intermediate class
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.
you can address it to pull the same way you do with a list. You can address it via indexing.
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.
In the advanced course. We do spend some time. We will spend some time on really deep diving into these data types,
much more so than we've done in this course of the previous course, where we're examining sort of under the hood
in sort of an abstract, theoretical way, how these data types were constructed.
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.
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
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
from a previous take. Yep.
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,
which we saw are the directory that we're examining
all subdirectories and all sub files.
It's going to perform some operation in this case. We're gonna use this for Luke. Just two prints,
And let's just see what this gives us.
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.
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,
but there are plenty of sub there plenty of files inside that directory.
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,
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
list every single item on the operating system
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
because it's sort of a shorthand for addressing items on the system,
and that is os dot system, and again, we're just gonna do a quick help on that to see what it's for.
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
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
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
changed the parent directory and then you contest and see where you are. With PWD,
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
is only going to be executed for as long as that single command takes.
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.
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,
is to take some time off and play with the tools that have been introduced.
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.
You can do the beginning part of our
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
Python. So thank you all for watching.
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.