Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Extracting Member Files and Closing ZIP Files

00:00 Extracting Member Files and Closing ZIP Files. Extracting the content of a given archive is one of the most common operations that you’ll do on ZIP files. Depending on your needs, you may want to extract a single file at a time or all the files in one go.

00:17 ZipFile.extract() allows you to accomplish the first task. This method takes the name of a member file and extracts it to a given directory signaled by path.

00:27 The destination path defaults to the current directory.

00:45 Now, new_hello will be in your output_dir/ directory. Note that .extract() returns the path to the extracted file.

00:56 If the target filename already exists in the output directory, then .extract() overwrites it without asking for confirmation. If the output directory doesn’t exist, then .extract() creates it for you.

01:08 The name of the member file must be the file’s full name as returned by .namelist(). It can also be a ZipInfo object containing the file’s information.

01:19 You can also use .extract() with encrypted files. In that case, you need to provide the required pwd argument or set the archive-level password with .setpassword().

01:32 When it comes to extracting all the member files from an archive, you can use .extractall(). As its name implies, this method extracts all the member files to a destination path, which is the current directory by default.

02:00 After running this code, all the current content of sample.zip will be in your output_dir/ directory.

02:11 If you pass a non-existing directory to .extractall(), then this method automatically creates the directory. Finally, if any of the member files already exist at the destination directory, then .extractall() will override them without asking for confirmation, so be careful when using this method.

02:28 If you only need to extract some of the member files from a given archive, then you can use the members argument. This argument accepts a list of member files, which should be a subset of the whole list of files in the archive at hand.

02:41 Just like .extract(), the .extractall() method also accepts a pwd argument to extract encrypted files. Sometimes it’s convenient for you to open a given ZIP file without using a with statement. In those cases, you need to manually close the archive after use to complete any writing operations and to free the acquired resources.

03:07 You can work with the ZIP file as seen on-screen.

03:14 The object will show that it’s open. You can then close the ZIP file using the .close() method on the ZipFile object, which will now show that it’s closed.

03:31 You must call .close() before exiting your program. Otherwise, some writing operations may not be executed. For example, if you open a ZIP file for appending ("a") new member files, then you need to close the archive to write the files.

03:46 The call to .close() closes the archive for you. In the next section of the course, you’ll look further into the creation of ZIP files, starting with the creation of compressed files and directories.

Become a Member to join the conversation.