python-zipfile

Create .zip in Python?

Create .zip in Python? Question: I’m trying to create a function in my script that zips the contents of a given source directory (src) to a zip file (dst). For example, zip(‘/path/to/dir’, ‘/path/to/file.zip’), where /path/to/dir is a directory, and /path/to/file.zip doesn’t exist yet. I do not want to zip the directory itself, this makes all …

Total answers: 3

get file list of files contained in a zip file

get file list of files contained in a zip file Question: I have a zip archive: my_zip.zip. Inside it is one txt file, the name of which I do not know. I was taking a look at Python’s zipfile module ( http://docs.python.org/library/zipfile.html ), but couldn’t make too much sense of what I’m trying to do. …

Total answers: 3

Does Python zipfile always use posixpath filenames regardless of operating system?

Does Python zipfile always use posixpath filenames regardless of operating system? Question: It probably won’t matter for my current utility, but just for good coding practice, I’d like to know if files in a ZIP file, using the zipfile module, can be accessed using a POSIX-style pathname such as subdir/file.ext regardless of on which operating …

Total answers: 2

Extract files from zip without keeping the structure using python ZipFile?

Extract files from zip without keeping the structure using python ZipFile? Question: I try to extract all files from .zip containing subfolders in one folder. I want all the files from subfolders extract in only one folder without keeping the original structure. At the moment, I extract all, move the files to a folder, then …

Total answers: 5

python zipfile module doesn't seem to be compressing my files

python zipfile module doesn't seem to be compressing my files Question: I made a little helper function: import zipfile def main(archive_list=[],zfilename=’default.zip’): print zfilename zout = zipfile.ZipFile(zfilename, “w”) for fname in archive_list: print “writing: “, fname zout.write(fname) zout.close() if __name__ == ‘__main__’: main() The problem is that all my files are NOT being COMPRESSED! The files …

Total answers: 3

Unzipping files in Python

Unzipping files in Python Question: I read through the zipfile documentation, but couldn’t understand how to unzip a file, only how to zip a file. How do I unzip all the contents of a zip file into the same directory? Asked By: John Howard || Source Answers: import zipfile with zipfile.ZipFile(path_to_zip_file, ‘r’) as zip_ref: zip_ref.extractall(directory_to_extract_to) …

Total answers: 9

How to create a zip archive of a directory?

How to create a zip archive of a directory? Question: How can I create a zip archive of a directory structure in Python? Asked By: Martha Yi || Source Answers: You probably want to look at the zipfile module; there’s documentation at http://docs.python.org/library/zipfile.html. You may also want os.walk() to index the directory structure. Answered By: …

Total answers: 28