- So far in this chapter, we've seen how to create files and how to get information about files. Python provides a set of utilities for manipulating files using the operating system's shell utilities. And that's what we're going to take a look at in this example. So let's start by opening up shell_start.py. And to use the shell utilities, I first need to import the Shutil module into my app. So let's go ahead and do that. Alright, import Shutil.
We'll start off by doing something pretty simple. Let's make a duplicate of an existing file. Now this example depends on textfile.txt existing. And if it's not there for you, it's probably because you didn't do one of the previous exercises in this chapter. And you just need to do with the previous exercises to make sure that this file exists. First the code uses the path modules exists function to make sure the file exists, and then I'm going to use the real path function to retrieve the path information for the file.
So I'll make a variable called source and I'll call path.realpath and I'll give it a name textfile.txt. Now to make a copy, I'm going to use the original path and just put .bak extension onto the end. So I'll right dst equals source plus .bak, and then I'll use the copy function of the shutil module to actually copy the file.
So I'll just give it the source and the destination. And that's all I need to do. So let's go ahead and run this. So I'll go to the debugger, and I'll click run. Alright, and we'll go back to the file list and sure enough you can see that the textfile.txt.bak got created. Now, the copy function only copies over the contents of the file. If you want to copy over things like the modification time and other metadata associated with the file, you need use the copy stat function.
So let's do that. So I'll write an shutil.copystat. Alright. And I'll get rid of this copyright here. Okay, and we'll save. And we'll run it again. Let's go back to the list. And sure enough there's the copy. And I'll leave it as an exercise to you to go back out to the finder or the file system and make sure that the metadata is the same as the original.
So now, let's rename the original file using the rename function. First let me comment out these guys right here. And I'm going to do this by using the OS module's rename function. And I'm going to rename textfile.txt to newfile.txt. And as you might imagine, this just renames the first specified file to have the second argument's name.
So let's go ahead and try that. So I'll save, and we'll run this. And sure enough, you can see that the file was renamed to newfile.txt. But using the shell is about more than just working with files. Let's try something a little more interesting. Let's put some content into a zip file. I'm going to comment out this line. And let me rename this file back to textfile.txt because otherwise this test is going to fail.
So in order to work with zip files, I'm going to use the shell utilities make archive function in order to create an archive file that contains the contents of this whole directory. And to do that, I need to import the make archive module. So I'll do that, I'll write from shutil, import make archive. So now, I need to get the directory path from the full file path. And I'll all use the split function for that.
So I'll create a root dir variable and a tail and I'll call path.split and remember we still have the source up here. Next, I need to call the make archive function with the name of the archive I want to create and the format I want to use which in this case will be zip. So I'll write shutil.makearchive, and I'll just call it archive and the format I want to be zip. It doesn't have to be zip, it can be tar, it can be some other compression formats that are supported but I'll just use zip.
And I'll pass in my root dir. So it will compress the entire directory. Let's go ahead and give that a try. So I'll run this. Alright, let's go back and look at the file list. And sure enough, you can see that the archive.zip was created and right now this archive contains all the contents of this directory. Now that's what happens by default, but I have more control over that if I want.
I can actually add specific items to the archive. So let's modify our code to do that. First I need to import the zip file module. So I'm going to write from zip file, import zip file. There we go, and next I need to add the code for creating the custom zip file. So let me comment out my previous example. And I'm going to write with zip file and this is a object constructor.
So I'm going to create a new zip file named testzip.zip and I'm going to open that for write access. And I'm going to say as new zip:, and that starts a new block. So the with key word lets me create a local scope that simplifies working with objects. So this code creates a new zip file object named test zip and I'm using the W parameter to indicate that want to write to the file.
The object I get back, I'm going to temporarily name my new zip variable. And then I can use that object to manipulate the data and write content to the file. So I'll write newzip.write and I'm going to add textfile.txt and also write newzip.write and I'll add textfile.txt.bak.
So essentially, I'm just going to add these two files to the archive. I save this, make sure that all the other things are commented out correctly. It looks at they are. So now, I'm going to run this. So I'll go over to the debugger, and I'll run. Alright, and let's go back to the file list and you can see now that there's a testzip.ZIP. So let's go ahead out to the file system and in my exercise files for chapter four, you can see that's testzip.zip right there.
I'll just double-click it and you can see that the folder that results has just these two items in it, textfile.txt and textfile.txt.bak. So you can see that using the shell utilities module of Python gives you some really great control over working with file objects.
- Installing Python
- Choosing an editor or 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