-3

well, I have a little knowledge in python and I try to zip a folder

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()

so, I get this answer but I don't understand the code very well and I don't know how to implement it in my own code, I mean where should I type my folder name, for example,Test_folder, where to type it in the code to implement a zip process in it, Can you rewrite the code and type test_folder in the true place in code.

M.lora
  • 1
  • 5
  • What have you tried to achieve your wanted result? Can you provide some code? – Geshode Jun 27 '18 at 09:16
  • Mix https://stackoverflow.com/questions/11523918/python-start-a-function-at-given-time and https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory – Polymer Jun 27 '18 at 09:17
  • @Polymer can you help me in that? – M.lora Jun 27 '18 at 13:00
  • I am not sure what the question is here. Other than some missing indents, the posted code is actually correct and will zip the directory and its sub-directories. – Leo K Jun 27 '18 at 13:03
  • @LeoK let's say I have a directory named as **test**, I want to zip it, in any place of code should I write **test** to zip the folder – M.lora Jun 27 '18 at 13:18
  • Have you tried `zipdir('test', zipf)`? Also make sure you indent your python code correctly. – Håken Lid Jun 27 '18 at 13:50

1 Answers1

0

You can add your folder name in this line.

zipdir('tmp/', zipf)
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Nagashayan
  • 2,487
  • 2
  • 18
  • 22