|
| 1 | + |
1 | 2 | importos |
2 | 3 | fromdatetimeimportdatetime |
3 | 4 | fromzipfileimportZipFile |
4 | 5 |
|
5 | 6 |
|
6 | | -# set file name and time of creation |
| 7 | +# # set file name and time of creation |
| 8 | +# today = datetime.now() |
| 9 | +# file_name = 'zipper_' + today.strftime('%Y.%m.%dh%H%M') + '.zip' |
| 10 | +# dir_name = 'tmp/' # update path |
| 11 | +# |
| 12 | +# |
| 13 | +# def zipdir(path, zip): |
| 14 | +# for root, dirs, files in os.walk(path): |
| 15 | +# for file in files: |
| 16 | +# zip.write(os.path.join(root, file)) |
| 17 | +# |
| 18 | +# if __name__ == '__main__': |
| 19 | +# zipfile = ZipFile(file_name, 'w') |
| 20 | +# zipdir(dir_name, zipfile) |
| 21 | +# zipfile.close() |
| 22 | + |
| 23 | + |
7 | 24 | today=datetime.now() |
8 | 25 | file_name='zipper_'+today.strftime('%Y.%m.%dh%H%M') +'.zip' |
9 | 26 | dir_name='tmp/'# update path |
10 | | - |
11 | | - |
12 | | -defzipdir(path, zip): |
13 | | -forroot, dirs, filesinos.walk(path): |
| 27 | +
|
| 28 | +
|
| 29 | +defadd_folder_to_zip(folderPath,zipFile): |
| 30 | +forroot, dirs, filesinos.walk(folderPath): |
14 | 31 | forfileinfiles: |
15 | | -zip.write(os.path.join(root, file)) |
16 | | - |
17 | | -if__name__=='__main__': |
18 | | -zipfile=ZipFile(file_name, 'w') |
19 | | -zipdir(dir_name, zipfile) |
20 | | -zipfile.close() |
| 32 | +print(file) |
| 33 | +print(root) |
| 34 | +full_path=os.path.join(root,file) |
| 35 | +zipFile.write(full_path) |
| 36 | + |
| 37 | +withZipFile("test_zip.zip","w") asmy_zip: |
| 38 | +print(my_zip) |
| 39 | + |
| 40 | +add_folder_to_zip('testfolder',my_zip) |
0 commit comments