Of course you need to have set up a logger using the logging module (see the other post).
Please be aware that copy and paste of the code below will most probably lead to indention error, as the code was more or less just a copy of respective passages in real code.
import lzma
def createLZMACompressedFile(fileNameIn, fileNameOut, logger, removeIn = False):
u"""Compresses in file to out file using LZMA.
The in file gets removed optionally.
"""
logger.debug("fileNameIn: {}".format(fileNameIn))
logger.debug("fileNameOut: {}".format(fileNameOut))
logger.debug("Current path: {}".format(os.getcwd()))
# Treat data as bytes to compress (wb and rb respectively)
with lzma.open(filename = fileNameOut,
mode = 'wb',
format = lzma.FORMAT_ALONE,
preset = lzma.PRESET_EXTREME) as fileOut:
with open(fileNameIn, 'rb') as fileIn:
data = fileIn.read()
fileOut.write(data)
if removeIn:
os.remove(fileNameIn)
A call could be done like this.
createLZMACompressedFile(fileNameIn = str(logFile),
fileNameOut = str(logFileNew),
logger = logger,
removeIn = True)
Keine Kommentare:
Kommentar veröffentlichen