I have a directory structure that is like /level1/level2/level3/level4/level5 and in level5 I have .json files I want to replace with zipped up versions so from /level1/level2/level3/level4/level5/{file1.json, file2.json, file3.json} to /level1/level2/level3/level4/level5/{file1.zip, file2.zip, file3.zip}
However my code generates the zip files in the folder where the script is, which is level 3, resulting in /level1/level2/[level3]{file1.zip, file2.zip, file3.zip}/level4/level5/{file1.json, file2.json, file3.json}
In addition if I unzip the file I get the entire directory structure instead of just the file. For example if I unzip file1.zip I get /level1/level2/[level3]{(/level1/level2/level3/level4/level5/file1.json), file2.zip, file3.zip}/level4/level5/{file1.json, file2.json, file3.json}
I've tried different arguments but I'm not sure how to get the result I want. How can I accomplish this?
This is my code currently
path = os.path.join( os.path.dirname(os.path.abspath(__file__)), level4, level5) for root, dirs, files in os.walk(path, topdown=True): print('This is root: ', root) for file in files: zf = zipfile.ZipFile( '{}.zip'.format(file[:-5]), 'w', zipfile.ZIP_DEFLATED) zf.write(os.path.join(root, file)) zf.close()