Tuesday, August 30, 2016

Python zipfiles with the cloud is technologic

Working with zip files in Python is straight forward.

Write it

s3connection = self.get_s3_connection()
bucket = s3connection.get_bucket(aws_bucket_name)
k = Key(bucket)
k.key = key.name

k.get_contents_to_filename(file_path)

Cut it, Paste it, Save it

import shutil
shutil.copyfile(self.src_zipfile, self.temp_zipfile)

Load it

zf = zipfile.ZipFile(zip_file)   # turn file into a zipfile object
zf.extractall(path=unpack_path)  # extract all files to path
files = glob.glob(os.path.join(unpack_path, 'list_*.txt'))  #glob glob
for files in files:
    # to stuff to the files here

Check it

# In a test case
zf = zipfile.ZipFile(zip_file)
zf.extractall(path=unpack_path)
files = glob.glob(os.path.join(unpack_path, 'list_*.txt'))
self.assert(len(files),expected_file_count)


Quick - rewrite it

with open(some_file, 'r+') as file:
    #do file rewrite here
    file.seek(0)
    file.write('Quick!')
    file.truncate()

Now here's daftfunk - technologic

No comments:

Post a Comment