zipfile

How to send zip files in the python Flask framework?

大城市里の小女人 提交于 2019-11-26 12:44:02
问题 I have a flask server that grabs binary data for several different files from a database and puts them into a python \'zipfile\' object. I want to send the generated zip file with my code using flask\'s \"send_file\" method. I was originally able to send non-zip files successfully by using the BytesIO(bin) as the first argument to send_file, but for some reason I can\'t do the same thing with my generated zip file. It gives the error: \'ZipFile\' does not have the buffer interface. How do I

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

旧时模样 提交于 2019-11-26 11:00:51
问题 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 remove previous subfolders. The files with same names are overwrited. Is it possible to do it before writing files? Here is a structure for example: my_zip/file1.txt my_zip/dir1/file2.txt my_zip/dir1/dir2/file3.txt my_zip/dir3/file4.txt At the end I whish

python/zip: How to eliminate absolute path in zip archive if absolute paths for files are provided?

大城市里の小女人 提交于 2019-11-26 10:41:02
问题 I have two files in two different directories, one is \'/home/test/first/first.pdf\' , the other is \'/home/text/second/second.pdf\' . I use following code to compress them: import zipfile, StringIO buffer = StringIO.StringIO() first_path = \'/home/test/first/first.pdf\' second_path = \'/home/text/second/second.pdf\' zip = zipfile.ZipFile(buffer, \'w\') zip.write(first_path) zip.write(second_path) zip.close() After I open the zip file that I created, I have a home folder in it, then there are

Create zip file: error running command “ ” had status 127

非 Y 不嫁゛ 提交于 2019-11-26 08:26:07
问题 I am trying to create a zip file from multiple files using the zip function in r, but I keep getting this error message: running command \'\"zip\" -r9X \"data.zip\" \"dt1.txt\" \"dt2.txt\" \' had status 127. How can I avoid that? setwd() dt1 <- sample(1:100, 10) dt2 <- sample(100:200, 10) write(dt1, \"dt1.txt\") write(dt2, \"dt2.txt\") zip(\'data.zip\', files =c(\'dt1.txt\', \'dt2.txt\')) 回答1: Here is how I solved this problem on my Windows 7 computer: Install Rtools from HERE. Locate the

python: Open file from zip without temporary extracting it

若如初见. 提交于 2019-11-26 08:14:21
问题 How can I open files from a zip archive without extracting them first? I\'m using pygame. To save disk space, I have all the images zipped up. Is it possible to load a given image directly from the zip file? For example: pygame.image.load(\'zipFile/img_01\') 回答1: Vincent Povirk's answer won't work completely; import zipfile archive = zipfile.ZipFile('images.zip', 'r') imgfile = archive.open('img_01.png') ... You have to change it in: import zipfile archive = zipfile.ZipFile('images.zip', 'r')

Unzipping files in Python

僤鯓⒐⒋嵵緔 提交于 2019-11-26 04:29:50
问题 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? 回答1: import zipfile with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref: zip_ref.extractall(directory_to_extract_to) That's pretty much it! 回答2: If you are using Python 3.2 or later: import zipfile with zipfile.ZipFile("file.zip","r") as zip_ref: zip_ref.extractall("targetdir") You dont need to use the close

How to create a zip archive of a directory in Python?

﹥>﹥吖頭↗ 提交于 2019-11-25 23:39:14
问题 How can I create a zip archive of a directory structure in Python? 回答1: As others have pointed out, you should use zipfile. The documentation tells you what functions are available, but doesn't really explain how you can use them to zip an entire directory. I think it's easiest to explain with some example code: #!/usr/bin/env python import os import zipfile def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(path): for file in files: ziph.write(os.path.join(root