import zipfile import tarfile import os ZIEL = '/home/ulrich/tmp' QUELLE = '/home/ulrich/1_privat' FOLDER = '64-UMKER' def untarFolder(): tar_file = tarfile.open(os.path.join(ZIEL, 'tartemp.tar.gz'), 'r:gz') tar_file.extractall(path=os.path.join(ZIEL, 'tarliste')) tar_file.close() pass def tarFolder(): with tarfile.open(os.path.join(ZIEL, 'tartemp.tar.gz'), 'w:gz') as tar_file: for folderName, subfolders, filenames in os.walk(os.path.join(QUELLE, FOLDER)): for filename in filenames: folderShort = folderName.replace(QUELLE + '/', '') # create complete filepath of file in directory filePath = os.path.join(folderName, filename) # Add file to zip tar_file.add(filePath, os.path.join(folderShort, filename)) tar_file.close() def unzipFolder(): zip_file = zipfile.ZipFile(os.path.join(ZIEL, 'temp.zip'), 'r') zip_file.extractall(path=os.path.join(ZIEL, 'liste')) zip_file.close() pass def zipFolder(): with zipfile.ZipFile(os.path.join(ZIEL, 'temp.zip'), 'w') as zip_file: # Iterate over all the files in directory for folderName, subfolders, filenames in os.walk(os.path.join(QUELLE, FOLDER)): for filename in filenames: folderShort = folderName.replace(QUELLE+'/', '') # create complete filepath of file in directory filePath = os.path.join(folderName, filename) # Add file to zip zip_file.write(filePath, os.path.join(folderShort, filename)) zip_file.close() return "" if __name__ == '__main__': zipFolder() unzipFolder() tarFolder() untarFolder()