Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Output multiple files to one zip file using Python Tool

Marta12
7 - Meteor

Hi All, 

I'm looking for solution to save 2 csv files to zip file using Python code (the command line with 7 zip or powershell solution won't work due to server  limitation). 

 

I have saved two .csv files in temporary location also want zip output in the same location with specific name and I'm giving this location in connection from Alteryx to Python Tool 

 

Input1:C:\Users\Marta\AppData\Local\Temp\1\Engine_24212_35fd6c49052f464195d34f3bff699af8_\file1_m.csv

Input2:C:\Users\Marta\AppData\Local\Temp\1\Engine_24212_35fd6c49052f464195d34f3bff699af8_\file2_m.csv

Output: C:\Users\Marta\AppData\Local\Temp\1\Engine_24212_35fd6c49052f464195d34f3bff699af8_\package_file

Path: Output: C:\Users\Marta\AppData\Local\Temp\1\Engine_24212_35fd6c49052f464195d34f3bff699af8_\

 

 

The Python code I'm trying to use 

 

import os, shutil

df = Alteryx.read("#1")

base_name=df['Output'][0]

root_dir=df['Path'][0]

base_dir=df['Input1']['0']

shutil.make_archive(base_name, zip, root_dir, base_dir')

 

 

[base_name] - full name of the .zip file to be created (including path but excluding file extension)

[root_dir] - folder where file you want to archive is located

[base_dir] - file name you want to archive

 

This code is working but only for one file, do you know how to modify it to save two files under the same zip? 

Also on the end the zip file is saved in different location- than base_name

 

I also find some option with Zipfile library but I don't know how to modify it 

 

import zipfile

with zipfile.ZipFile(zipname,'a') as myzip:

name_file_only=filename.split(os.sep)[-1]

myzip.write(filename,name_file_only)

os.remove(filename)

 

 

I will be grateful for any tips and solution. 

2 REPLIES 2
kelly_gilbert
13 - Pulsar

Hi, @Marta12 - here's how to add multiple files to a zip file using zipfile:

 

from os import path
from zipfile import ZipFile

with ZipFile(output_zip_path, 'w') as zipObj:
   zipObj.write(file1_path, path.basename(path.normpath(file1_path)))
   zipObj.write(file2_path, path.basename(path.normpath(file2_path)))
   zipObj.write(file3_path, path.basename(path.normpath(file3_path)))

 

In this example, output_zip_path is the full path of the zip file to be created. This would be similar to your base_name variable (but it should include the .zip extension).

 

zipObj.write writes each file to the zip.

The first argument is the full path to the file (e.g. C:\Users\Marta\AppData\Local\Temp\1\Engine_24212_35fd6c49052f464195d34f3bff699af8_\file1_m.csv)

The second argument returns just the filename (e.g. file1_m.csv)

 

Add as many zipObj.write lines as needed for the number of files you want to write out. 

 

 

Also, I don't quite understand how you are feeding the file paths to the Python tool... Are they hard coded, or are the file paths in df?

Marta12
7 - Meteor

Thank you @kelly_gilbert , Yours answer helps but I didn't use it fully. 

In the end I needed 3 zip files which each contains 2 csv files inside. 

 

The path of files I generate in Alteryx workflow and passed it to Python with "df" (#1) connection. This is how it looks

Marta12_0-1648643864665.png

 

Results of Cross Tab Tool:

Marta12_1-1648644018871.png

 

OutputPathzip- is the path I wanted files to be saved with the name and zip extension

Column 1 & 2 have a path to two csv files

So each row define the zip file and the files that need to be inside. 

 

The Python code I used:

 

 

from ayx import Alteryx
import pandas
import zipfile
from zipfile import ZipFile

df = Alteryx.read("#1")

for i in range(len(df)):
source=df['1'][i]
source2=df['2'][i]
root_dir = df['OutputPathzip'][i]
zipObj = ZipFile(root_dir, 'w')
zipObj.write(source)
zipObj.write(source2)
zipObj.close()

Marta12_2-1648644264847.png

 

 

 

Labels