My program so far is successfully able to add and compress files which are present in the same directory as test.py
but there is also a folder named location
in the same directory which contains additional files say citynames.txt
What happens is when I do a simple zip.write (The logic used in else
block) location
is added to the zip file however its empty that is it does not contain citynames.txt
inside it for some reason please help me add the folder itself somehow?
import os
import zipfile
import shutil
dir_path = os.path.dirname(os.path.realpath(__file__)) #holds the directory where script is located
os.chdir(dir_path) #Changes to directory where script is located
fp=os.listdir(dir_path) #file pointer
directory_size=len(os.listdir(dir_path))
zip1=zipfile.ZipFile('Archive.zip','w')
for i in range(directory_size) :
if fp[i]=='test.py':
break
if fp[i]=='location':
#Some code needs to be added here to write all the contents of folder "location" into
"Archive.zip"
else:
zip1.write(fp[i],compress_type=zipfile.ZIP_DEFLATED)
print("Process completed")
Assuming location has further sub-folders how to zip the files inside them aswell?