I've seen a few responses near what I'm after, yet my code is still not working. I've peppered it with print statements to see what each stage is doing and this (theoretically!) is actually what I'm after, yet I'm not getting the result.
Trying to FOR LOOP multiple files to a ZIP archive using WITH statement (yet no luck)
def files_rezip():
num = 1
for i in os.listdir():
x = os.path.splitext(i)
if 'temp' in x[0]:
new_dir = os.getcwd() + '\\' + i
# (ABOVE IS CONTEXT) - BELOW IS WHERE THE ISSUE IS...
with zipfile.ZipFile(os.getcwd() + '\\' + 'new_zip_'+ str(num) + '.zip', 'w') as new_file:
for nf in os.listdir(new_dir):
new_file.write(nf)
num += 1
print('Task Complete')
I've also tried:
new_file.write(new_dir + '\\' + nf)
The issue seems to be in the line:
new_file.write(nf)
I've tried the above - but this is just the file name without a path
This does provide the absolute path and helps it locate the files correctly - but this also causes the zip to recreate the whole file path IN the ZIP file?!