파일 및 폴더, 압축 포장의 규정

요구 사항 : 회사는 패키지와 로그를 압축 할 필요가, 로그는 두 가지 형식으로 나누어 져

첫 번째 : 2019-10-12.log (파일에 속하는, 당신은 하루에 압축 할 필요가있다)

두 번째 : 2019-10 (폴더에 속하는 지난달의 폴더가 압축 넣어)

모두가 여기에 스크립트에 통합, 매일 실행할 수 있습니다. 두 번째 경우가 발생하면,이있는 경우, 그렇지 않은 경우, 건너 뛰기, 패키지를 압축됩니다, 지난 달의 폴더의 어떤 부분이없는 것으로 판단

수입 OS는
 가져 ZipFile를
 수입 , 시간을 날짜를
 에서 dateutil.relativedelta 수입 relativedelta

# ################################################# ####################### 
# 파일을 이름을 : log_zip.py 
#은 2019년 11월 25일 16시 30분 46초 다음에 생성 
# 저자 : xieys 
# 마지막 수정 : 2019년 11월 25일 16시 30분 46초 
# 설명 : 
# 수요 : 로그 파일을 패키징 할 필요가, 두 가지 형식이있다 
# 2019-11-24.log (파일 첨부) : 첫 번째 양식 
#의 두 번째 형식 : 2019-10 (폴더에 속하는) 
# ###################################### ################################## 

tanyu_writeLog = R & LT ' D : \ IIS_html \ KMMicro_Food_Tanyu \ WRITELOG ' 
wcf_tanyu = R & LT ' D : \ IIS_html \ KMMicro_WCF_Tanyu \ 로그 \ INFO \ 2019 '
infomation_tanyu_nlog = R ' D : \ IIS_html \ KMMicro_Infomation_Tanyu \ 로그 \ NLog \ 디버그 ' 
infomation_tanyu_debug = R ' D : \ IIS_html \ KMMicro_Infomation_Tanyu \ 로그 \ 디버그 \ 2019 '

DEF (경로 strfdate)를 tar_log :
     만약 strfdate == ' % Y- % M- % D ' :
        LOG_FILE = ' % s.log ' % ((datetime.date.today () + datetime.timedelta (일 = -1 )).의 strftime (strfdate))
        base_file = os.path.join (경로 LOG_FILE)
         경우 os.path.exists (base_file)
            zip_file = zipfile.ZipFile ( ' % s.zip ' % base_file, ' w ' , zipfile.ZIP_STORED, 참)
            os.chdir (경로)
            zip_file.write (LOG_FILE, compress_type = zipfile.ZIP_LZMA)
            zip_file.close ()
            os.remove (base_file)
    다른 사람 :
        만약 DIR_Name = os.path.join (경로 (datetime.date.today () - relativedelta (개월 = + 1 ).)의 strftime (strfdate))
         경우 os.path.exists (만약 DIR_Name) os.path.isdir (만약 DIR_Name ) :
            zip_file = zipfile.ZipFile ( ' % s.zip ' % 만약 DIR_Name, ' w ' , zipfile.ZIP_STORED, 참)
            os.chdir (경로)
            file_ls = tar_dir (만약 DIR_Name [])
             에 대한 I 에서 file_ls :
                LOG_FILE = i.replace (경로 + " \\ ' , ' ' )
                zip_file.write (LOG_FILE, compress_type = zipfile.ZIP_LZMA)
                 경우 os.path.isdir (내가) :
                     계속 
                다른 사람 :
                    os.remove (I)
            zip_file.close ()
            emp_dir = 반전 (tar_dir (만약 DIR_Name []))
             에 대한 I 에서 emp_dir :
                 시도 :
                    os.removedirs (I)
                제외시켰다 전자로 예외 :
                     패스를


        다른 :
             패스


데프 tar_dir (경로, file_list를) :
     '' '
    재귀 적으로 모든 파일과 폴더의 경로를 얻을 후 반환
    궁극적 인 경로 :
    : PARAM의 file_list를 :
    :반환:
    '' ' 
    에 대한 I 에서 os.listdir (경로) :
        파일 = os.path.join (경로, 나는)
         경우 os.path.isdir (파일) :
            file_list.append (파일)
            tar_dir (파일, file_list를)
        다른 사람 :
            file_list.append (파일)
            # file_list.append (os.path.join (os.path.basename (경로), I)) 
    복귀 를 file_list




경우  __name__ == ' __main__ ' :
    tar_log (tanyu_writeLog, ' % Y- % M- % D ' )
    tar_log (wcf_tanyu, ' % Y- % m ' )
    tar_log (infomation_tanyu_nlog, ' % Y- % M- % D ' )
    tar_log (infomation_tanyu_debug, ' % Y- % m ' )

 

추천

출처www.cnblogs.com/xieys-1993/p/11933380.html