Example of how to make and use a ZipArchive

ZipArchives are a powerful tool for collecting files and managing data

In [1]:
# first we import the ZipModels module.
from pyMez.Code.DataHandlers.ZipModels import *
Importing pyMez, this should take roughly 30 seconds
Importing Code.DataHandlers.GeneralModels
It took 2.529 s to import Code.DataHandlers.GeneralModels
Importing Code.DataHandlers.HTMLModels
It took 0.42 s to import Code.DataHandlers.HTMLModels
Importing Code.DataHandlers.NISTModels
It took 5.855 s to import Code.DataHandlers.NISTModels
Importing Code.DataHandlers.TouchstoneModels
It took 0.008 s to import Code.DataHandlers.TouchstoneModels
Importing Code.DataHandlers.XMLModels
It took 0.214 s to import Code.DataHandlers.XMLModels
Importing Code.DataHandlers.ZipModels
It took 0.048 s to import Code.DataHandlers.ZipModels
Importing Code.InstrumentControl.Experiments
It took 1.029 s to import Code.InstrumentControl.Experiments
Importing Code.InstrumentControl.Instruments
It took 0.011 s to import Code.InstrumentControl.Instruments
Importing Code.Utils.Names
It took 0.009 s to import Code.Utils.Names
It took 10.123 s to import all of the active modules
In [2]:
# a new zip archive
new_zip=ZipArchive()
In [3]:
# it has a default name
new_zip.path
Out[3]:
'Data_Archive_20180714_002.zip'
In [4]:
# we can add a file to the zip archive
file_to_add=r"./Zip_Example_Files/WR15_isolation.s2p"
In [5]:
# just provide the path to the file to add
new_zip.add_file(file_to_add)
In [6]:
# now the files are listed in the attribute files
new_zip.files
Out[6]:
['WR15_isolation.s2p']
In [7]:
# we can add files of any type
new_zip.add_file(r"./Zip_Example_Files/WR15_Compare_Corrected_Loads.png")
In [8]:
new_zip.files
Out[8]:
['WR15_isolation.s2p', 'WR15_Compare_Corrected_Loads.png']
In [9]:
# we can add all the files in a directory
new_zip.add_all_from_directory(r".\Zip_Example_Files\Mulitple_Files")
In [10]:
new_zip.files
Out[10]:
['WR15_isolation.s2p',
 'WR15_Compare_Corrected_Loads.png',
 'Thru_WR15_20180215_001.s2p',
 'Thru_WR15_20180215_002.s2p',
 'Thru_WR15_20180215_003.s2p',
 'Thru_WR15_20180215_004.s2p']
In [11]:
# we can create a string and add it as a new file
new_zip.write_string(data_string="A new fake file",archive_file_name="string.txt")
In [12]:
# lets save the archive
new_zip.save(os.path.join(os.getcwd(),r"Zip_Example_Files\ZipArchive_Test_01.zip"))
In [13]:
new_zip.files
Out[13]:
['WR15_isolation.s2p',
 'WR15_Compare_Corrected_Loads.png',
 'Thru_WR15_20180215_001.s2p',
 'Thru_WR15_20180215_002.s2p',
 'Thru_WR15_20180215_003.s2p',
 'Thru_WR15_20180215_004.s2p',
 'string.txt']
In [14]:
# now we can extract all of the files to a directory
new_zip.extract_all(destination_directory=r"Zip_Example_Files\Extracted_Files")
In [ ]:
# now we close it and it goes away. Note, because of the way zip files work a copy will be saved under the default name.
# To change the default name set the options directory, specfic_descriptor, general_descriptor