Last active
September 30, 2015 09:34
-
-
Save ujuc/4cc9a4e51880aee37481 to your computer and use it in GitHub Desktop.
logfile 압축
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8- | |
import os | |
import re | |
import sys | |
import tarfile as tar | |
LOG_PATH = sys.argv[1] | |
def file_tar(logfile_name): | |
""" | |
compress files | |
""" | |
tarfile = logfile_name + ".tar.gz" | |
out = tar.open(tarfile, 'w:gz') | |
try: | |
out.add(logfile_name) | |
finally: | |
out.close() | |
if __name__=='__main__': | |
os.chdir(LOG_PATH) | |
ls = os.listdir(LOG_PATH) | |
logfiles = [] | |
# find logfiles | |
for files in ls: | |
if bool(re.findall(r"log.+", files)) and not bool(re.search(r"gz", files)): | |
logfiles.append(files) | |
print "{} is compress.".format(files) | |
logfiles.sort() | |
# compress files and delete log file | |
for logfile in logfiles: | |
file_tar(logfile) | |
os.system('rm -f {0}'.format(logfile)) | |
print logfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding=utf8 -*- | |
import os | |
import re | |
import tarfile as tar | |
import sys | |
from datetime import date | |
LOG_PATH = sys.argv[1] | |
def file_tar(logfile_name): | |
""" | |
compress files | |
""" | |
tarfile = logfile_name + ".tar.gz" | |
out = tar.open(tarfile, 'w:gz') | |
try: | |
out.add(logfile_name) | |
finally: | |
out.close() | |
return True | |
return False | |
if __name__=='__main__': | |
os.chdir(LOG_PATH) | |
ls = os.listdir(LOG_PATH) | |
logfiles = [] | |
# find logfiles | |
for files in ls: | |
if bool(re.match(r"{}.\d".format(sys.argv[2]), files)) and not bool(re.search(r"gz", files)) and not bool(re.search(r"{}".format(str(date.today())), file)): | |
logfiles.append(files) | |
print logfiles | |
# compress files and delete log file | |
for logfile in logfiles: | |
file_compress = file_tar(logfile) | |
if file_compress: | |
os.system('rm -f {0}'.format(logfile)) | |
print "Delete log {}".format(logfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment