пожалуйста, возвращайтесь позднее
пожалуйста, возвращайтесь позднее
>> PARLANTE: So, in this section, I want to play up this idea of modules of existing code that you might want to use to just sort of solve common problems. In this case, I'm going to show you some file system interface stuff and also how you call an external process and capture its output and the like to do something. So you can imagine using Python sort of — you might use Bash but just — it's sort of a better Bash to sort of glue something together some sort of — some sort of utility. So I'll start off in the interpreter here, fire up Python. And the first module I want to talk about is the OS module since we're operating system, I think, and I'm just going to do a DIR on it. So I import the OS module. I'm going to look inside of there, and you can see there are all sorts of functions in there. There's obviously, you know, "setpgid" and "nice." There are obviously kind of operating system-oriented utilities, have a very kind of a UNIX-y feeling. In theory, these are — these try to be platform-independent. So if you write a — wrote a Python program and it's running on Windows, some of these are stubbed out where you could call, you know, and try to get the current time or whatever, and it's going to translate it. I don't believe it's done perfectly, but it tries. So going through those, there's a — there's at least, theoretically, a degree of platform independence. So I would like to show you a couple — so obviously, there's tons of stuff in here. The one I'd like to show you for starters is listdir. That one. So, actually, I could do help on it. Just to show you how that works, so I say, "os.listdir." So it says — okay, what this does, it takes — nice summary — it takes a path and it's going to give me a list of strings. So what it's going to do is I give it a path to a directory and then it's going to figure out what all the filenames are in that directory and just return it to me as a list of Python strings. So let me go to the interpreter here. So to demonstrate this, what I thought I'll do is I'll modify the long-suffering hello.py example to just, you know, I don't know, list files. So I'll say "import os" here. I'll rename this list, upper case L. Let's say this will take a directory. So let's see, I'll say, "filenames = os.listdir (dir)" and I just sort of as I've been encouraging you to do for the exercise — well, I'll just print what that gives me for starters. So here I'll say, lists are here in the main. I'll just leave it the way it is. So I'll just assume that there's one command line argument and I'll just list it. So, hopefully, it's in the same directory. So if I say, "hello." and you could see it's a, you know, it's found so I'll do an ls; that way, we can access this information. So there's this . DS_Store thing that the Macintosh, like, pathologically puts everywhere, and other than that, you'll see there's just kind of regular file names. So let me make this code do something a little more interesting. So at least I printed that it's there. So here, I'll — let's loop through them. So I'll say, "for filename in filenames:" sort of typical kind of thing. So one thing I can do, if I want to make a path out of this — but what's important to understand is that when you do a listdir to get file names out of a directory, just that filename on its own, just out in space, is not a valid path, right? It needs to be connected to the directory it came from to make a valid path.