Intermediate Python

Practice: Standard Library

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Practice: Standard Library" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina reviews how to interact with several common packages, utilizing the 'as' keyword, and introduces how to use content manager.

Preview
Close

Transcript from the "Practice: Standard Library" Lesson

[00:00:04]
>> Nina Zakharenko: In this exercise on the standard library, you're gonna use the OS package and the standard library to get a little bit more information about our local directory and print out its contents. You're also going to use the sys package to get information about our system, what platform we're on, and also accept arguments into our Python scripts.

[00:00:31]
So using the standard library I'm going to make a new file here.
>> Nina Zakharenko: And I'm gonna import os, and I'll save that here, just in my pyworkshop, call it lib_example. So oops, I did the wrong thing there.
>> Nina Zakharenko: I want to do a file, save as, want that to be a .py, otherwise VS Code doesn't know.

[00:01:16]
Okay, so import os, what does os in the standard library do, what is it for?
>> Speaker 2: Operating system functions.
>> Nina Zakharenko: Operating system functions. So I can say, my_folder =, and I can use the os module to get the cwd. CW stands for current working directory. And then I can print out, Here are the files in:.

[00:01:49]
Let me make that an f string and say, {my_folder}. So this is gonna be the name of the folder. Let me quickly run this. Sorry, not Start REPL. I'm gonna Select Run Python File in Terminal. And because I started a new REPL session in this terminal that VS Code doesn't really know what to track, I'm gonna have to hit CTRL + C to exit the REPL.

[00:02:20]
And then I should be able to run my file in the terminal. So here is the full path, and then if I wanted to loop through this folder, I can say with os.scandirectory my_folder, as folder. And we haven't seen this with syntax just yet, but what this is, is a context manager.

[00:02:47]
And after the code in this block is done running, the context manager is automatically going to do some code clean up. So generally a good idea when we're working with files because the system will wanna close the handles to those files. So you'll also see this with syntax when working with or writing to files.

[00:03:11]
Okay, so I'm using the as label. We're gonna get the contents of scanning this directory and assign it to folder. So I can say for entry in folder, print out an f string of the name of the entry.
>> Nina Zakharenko: And if I run this in my terminal, you'll see all of the files here, the top level files in that directory.

[00:03:48]
>> Nina Zakharenko: Sys is another useful library. It's gonna give you access to variables and functions, user maintained by Python interpreter. So I'm gonna make a new file here. I'm gonna save it as sys_example.py. Here I'm gonna import sys. And there is a variable called sysy.argv that is always set to the command line arguments used to run this script.

[00:04:18]
So I can say, I'm gonna print out, We received these arguments. And I'm just gonna print them out.
>> Nina Zakharenko: And then I can also say, I'm gonna print out an f string and say, We're currently running on a, and use sys.platform, which will be set to the OS that you're using machine, okay.

[00:04:54]
So if I run this in the terminal, I'll see that we received these arguments, and there's already one value in this list even though we didn't pass anything in.
>> Nina Zakharenko: So running that again without providing the full path, whati s this first argument?
>> Speaker 2: It's the filename.
>> Nina Zakharenko: It's the filename.

[00:05:22]
So sys.argv is never empty, the zeroth element is always the name of the file. And after that I can pass in whatever values I want. And we'll see that we received these arguments. And each of those space separated arguments gets passed into sys.argv. Notice that even though I passed in some integers here, the arguments are always strings.

[00:05:53]
>> Nina Zakharenko: Okay, using PyPi, if we don't wanna open the website and we kind of wanna deal a little bit of exploration, PyPi has really helpful search method. So I could say, python-m pip. And pip is the tool associated with PyPi. And I can say pip search request. And this will go and get me a list of the packages that have requests in the name.

[00:06:27]
If I want to install a package, I could say python -m pip install requests. And I already have it installed, but then it will go ahead and do that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now