Join Joe Marini for an in-depth discussion in this video Using file system shell methods, part of Learning Python (2013).
Python provides a set of utilities for manipulating files using the Operating System's shell utilities. And that's what we're going to look at in this example. So here in my example snippets file I scroll down to the shell section. And let's start by opening up shell_start.py. So from the snippets, let's copy and paste these three lines right here. And we'll paste them at the top. And, let's start off doing something pretty simple. Let's make a duplicate of an existing file.
Okay. So let's copy these lines to begin with and paste them into the main. Okay. Now this example depends on text_file.txt existing, and you can see I've got it over here in my list. if it's not there for you it's probably because you didn't do one of the previous exercise. So you should go do one of the previous exercises to make sure that this file exists right here. All right, so what we're going to do is use the path module's exists function to make sure the file exists.
And then assuming that it exists, and you can see that it does, we're going to get the path to that file and store it in the source variable right here. So once I've done that I'm going to get the separated path and file name. So, here I'm going to use the split function on the source variable to get back the separated path from the file name. So, let's just run that and see what we have so far.
Let's save okay, so you can see that I've got the path and the file name separated out, alright so far so good, okay. What we're going to do is make a duplicate of it and give it a new name. So let's copy these lines right here and go back in and paste. So I'm going to take the original file name textfile .txt and put the letters .bak on the end to indicate that it's a backup copy. And then I'm going to use the shell utility's copy function. Right here on the shutil module to copy from the source to the destination. So, basically the destination variable is the same as the source just with .bak on the end.
So, let's run that. All right, and you can see over here in the list sure enough textfile.txt.bak got created all right. So that's how you use the shell to make a copy of the file, and let's go back into the snippets. Now the copy function only copies over the contents of the file. If you want to copy over things like the file permissions and the other meta data associated with the file like the modification time and so on. You need to use the copy stat function.
So we'll copy that and we'll paste it in over here, paste, alright lets save. Now lets go delete this file so we can do the whole thing all over again. Alright. So let's run this. Okay. And you can see that textfile.txt.bak was created. And if you go look at the properties of that file in your operating system shell, you'll see that all the other metadata was copied over as well.
I'm not going to do that here because its beyond the scope of the exercise, but, you know, if you want to do some extra credit go look at the file in your OS shell. All right. Let's go back to the snippets. Now let's rename the original file. So we'll copy these lines here and down here at the bottom. (SOUND) All right. So let's comment out all the stuff that we used before because we're not going to need it again. So what I'm doing here is using the rename function on the OS module to rename textfile.txt to newfile.txt.
All right, so we'll run this. And let's go over here and Refresh. And you can see, sure enough, that textfile.txt was renamed newfile.txt, okay? So, we can use the shell to rename things, that's pretty cool. All right. Let's do something even cooler. Let's put stuff into a ZIP archive. So, I'm going to copy these lines here and I'm going to comment out the renamed function and I'm going to paste these lines in. Alright, let me scroll down a little bit so you can see what's going on. Okay.
So we're going to use the shell utilities make archive function in order to create an archive file that contains the contents of the directory. And in order to do that I'm going to need to import the make archive class from the shell utilities. I'll copy that, scroll up here and paste. All right. So what we're doing is we're using the split function to split out the directory and the file name from the full path to the location of the textfile.txt file. And actually since we just renamed that, we should probably fix this problem by changing this to say newfile.txt.
Okay. So now that code will work. So what we're going to do is get the path to that file, split it into a directory and a tail, and then we're going to call on the shellutil module, the make archive function. This is the name of the file that will be created. It's going to be called archive. The type that we want to create is a zip archive. And Python lets you create different kinds of things. You can create Tar files for Unix, and so on. But for the purposes of this example, we're going to create a zip file. And then we pass in the root directory of things we want to be zipped up.
So, everything in this directory, the directory where the file's located is going to be zipped and put into an archive. So, let's save. All right, let's run it. Okay, let's Refresh. And you can see that archive.zip got created. So I am going to right click on this and choose show in Window's Explorer. And, this may look different for you on Mac or Linux, but, the show in feature should be there. And you can see right here, here's archive.zip, so I'm going to open it up. And you can see that everything in the folder, all the example files, the text files, everything got added to the zip file.
Now that's not exactly what I wanted. I only wanted to put the text file and txt.bak file into the archive. So lets close this. Lets go back into Aptana Studio . Lets delete this archive. Okay. Alright. I want to have more control over my zip file, so what I'm going to do is, in example snippets I'm going to import the zipfile class from the zip file Python module.
And this module gives me full control over creating zip files. So I'm going to paste that in up here, save. Alright let's go back to the snippets and let's do some more fine grained control over my zip file. Alright. So I'm going to paste this in down here and I'm going to comment these guys out. Alright. So by important this zip file class I can now have much greater control over creating zip files, so here's what I'm going on line 36. I'm saying with, and with is a Python construct that creates its own local scope for working with objects.
So, what I'm saying is, with creating a new ZipFile class and a creator file called testzip.zip, I need to pass in the permissions I want, because it's a file. So I want to write information into the file as newzip. Now when I say as newzip, inside this with scope block I can use this variable to refer to a zip file that I just created. So what I'm going to do is say newzip.write, we use the write function on the newzip variable. Which is a zip file class object, I'm going to write the newfile.txt file, and I'm going to write the backup file that we created into that archive.
So now, I'm not going to add all the example files, just the files that I want to be in the archive. So, I'm going to save, and I'm going to run. Okay. And we'll give it a second to refresh, I'll just refresh it. Okay. So you can see there's test zip.zip. And by the way I want to point out that the reason why I'm not closing off the zip file here with you know newzip.close. For example is because inside this width scope lock, when the flow of control the program falls outside of the scope. The file will be already cleaned up and automatically closed for me. It's just something that Python does for you when you use scope locks like this. Okay.
So let's go out and look at the zip file. So once again we'll show it in the OS. So there's test zip.zip. If I open it up, you'll notice that only those two files were included in the zip archive. So you can see that using the shell utility's module of Python gives you really great control over working with file objects.
- Installing Python
- Choosing an editor/IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching Internet data
- Parsing and processing HTML