I'm trying to compress a folder with all files inside it
but I get the compressed file empty
Im using this code
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, file))
if __name__ == '__main__':
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
Related
import os
from zipfile import ZipFile
from os.path import basename
src = "C:\git\mytest"
full_path=[]
for root, dirs, files in os.walk(src, topdown=False):
for name in files:
full_path.append(os.path.join(root, name))
with ZipFile('output.zip', 'w') as zipObj:
for item in full_path:
zipObj.write(item, basename(item))
Trying to create a zip file with containing some file of a specific folder.
In specific folder has some files. then it will add to zip file
In the mentioned code, one zipfile is created but there is no file. I am not getting the exact reason
I am trying to zip all the files from a folder and places it in the same folder. Below is my code:
import os as __os
import zipfile
def zipdir(path, ziph):
for root, dirs, files in __os.walk(path):
for file in files:
print(file)
ziph.write(__os.path.join(root, file))
if __name__ == '__main__':
zip_name = 'test.zip'
working_directory = "C:\\Users\\Admin\\Downloads\\gaz"
archive_name = __os.path.join(working_directory, zip_name)
with zipfile.ZipFile(archive_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
zipdir(working_directory + '', zipf)
I am trying to create zip of all the files inside a folder. I am expecting the code to create a zip in the same folder. Its going into infinite.
I don't really see an endless loop but you're including the newly created test.zip (empty) to the archive of the same name.
It would be better to check file before adding it to the archive:
def zipdir(path, ziph):
for root, dirs, files in __os.walk(path):
for file in files:
if file != zip_name:
print(file)
ziph.write(__os.path.join(root, file))
The zipdir() funciton is calling itself recursively, resulting in the infinite loop. Try zipf.write(). That function adds a file to a zip archive.
with zipfile.ZipFile(archive_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in __os.walk(working_directory):
for file in files:
if os.path.samefile(archive_path, os.path.join(root, file)):
continue
zipf.write(__os.path.join(root, file))
Edit: Check if archive_name file is located in working_directory
I found out how to do it with this script:
import os, sys, shutil, glob
if not os.path.exists('Files'):
os.makedirs('Files')
source_dir = os.path.join(os.environ["HOMEPATH"], "Desktop")
dest_dir = 'Files'
try:
for root, dirnames, filenames in os.walk(source_dir):
for file in filenames:
(shortname, extension) = os.path.splitext(file)
if extension == ".txt" :
shutil.copy2(os.path.join(root,file), os.path.join(dest_dir,
os.path.relpath(os.path.join(root,file),source_dir)))
except FileNotFoundError:
pass
But, it only copies the files in the path and not the in sub-folders
So for example here it copies the desktop .txt files, but it does not copy the files in the folders.
Thanks, have a good day.
I have a bunch of folders full of data and I need specific ones to be sent to a zip folder. I can move them with a batch file but it does not automatically zip and then I have redundancy. I also tried a solution I saw.
import os
import zipfile
def zipit(folders, zip_filename):
zip_file = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
for folder in folders:
for dirpath, dirnames, filenames in os.walk(folder):
for filename in filenames:
zip_file.write(
os.path.join(dirpath, filename),
os.path.relpath(os.path.join(dirpath, filename),
os.path.join(folders[0], '../..')))
zip_file.close()
folders = [
"\Users\andria.baunee\Desktop\New folder",
"\Users\andria.baunee\Desktop\New folder (2)",
"\Users\andria.baunee\Desktop\New folder (3)"]
zipit(folders, "\Users\andria.baunee\Desktop\hooray.zip")
Nothing seemed to happen.
I want to put a file in every file of .txt, .mp3, and mp4 files.
import zipfile
import os
path = "G:"
file_zip = zipfile.ZipFile(path+'\\archive.zip', 'w')
for folder, subfolders, files in os.walk(path):
for file in files:
if file.endswith('.jpg,.txt,.mp3,.mp4'):
file_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), path), compress_type = zipfile.ZIP_DEFLATED)
file_zip.close()
When I open the zip file, the zip file is empty!
Try changing:
if file.endswith('.jpg,.txt,.mp3,.mp4'):
to:
if file.endswith(('.jpg', '.txt', '.mp3', '.mp4')):