Reputation: 53933
I've got some files in /tmp/1234/
and I'm creating a zip using Python:
def create_local_zip_file(zipjob_number, folder_path):
zip_file_path = os.path.join('/tmp/', f'{zipjob_number}.zip')
with ZipFile(zip_file_path, 'w') as zip_obj:
for filename in glob.glob(os.path.join(folder_path, "*")):
zip_obj.write(filename)
return zip_file_path
This creates a zip, but when I unzip the created zip file I get a folder called tmp
which in turn contains a folder called 1234
which then contains the files.
How can I make sure the unzip creates a folder called 1234
containing the files directly?
Upvotes: 0
Views: 831
Reputation: 71
It looks like each of your filenames includes the folder, which is why they're being written in to a directory within the zipfile.
You can use the "arcname" parameter to give the files a different name within the archive
I can see a way to do this with string splitting, however can I suggest using pathlib instead. Something like:
from pathlib import Path
...
for file in Path(folder_path).glob("*"):
zip_obj.write(file, arcname=file.name)
...
Upvotes: 1