'Passing variable to zipfile NAME
I want to assign to my zipfile from a variable named file_path
, how I can do that? I tried with the code below, and this line is the problem:
with ZipFile('%s.zip' %(file_path,),'w') as zip:
Nothing happens; I get no errors just Python doesn't create the .zip
file. Here is all the code.
import os
from zipfile import ZipFile
file_paths = []
basepath = 'my_directory/'
with os.scandir(basepath) as entries:
for root, directories, files in os.walk(basepath):
for entry in entries:
if entry.is_file():
file_path = os.path.join(entry)
file_paths.append(file_path)
with ZipFile('%s.zip' %(file_path,),'w') as zip:
print("FILE:", entry.name)
for entry in file_paths:
zip.write(entry)
Solution 1:[1]
I stumbled upon this unanswered query while I was go though something else, a simple work around would be to use .format
as below
with ZipFile('{}.zip'.format(d),'w') as zip:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vikram Karthic |