玩蛇网提供最新Python编程技术信息以及Python资源下载!

python方法备份桌面上非link文件

python 培训

python方法备份桌面上非link文件.需要用到os、shutil、logging、sys等模块方法。文件备份技巧python源码类文章会陆续更新。

#!/user/bin/python

import os
from datetime import date, datetime, timedelta
import shutil

import logging
import sys

class BkpDT:

    def __init__(self):
        self.exculdeFileExt = ['.lnk']
        self.logger = self.getLogger(sys.argv[0])
        self.backupFolder = '/'.join(['D:/BackupFiles', str(date.today())])


    def getLogger(self, carrier):
        logger = logging.getLogger(os.path.basename(carrier))
        logger.setLevel(logging.DEBUG)
        conlog = logging.StreamHandler()
        formatter = logging.Formatter("%(asctime)s - %(name)s - %(lineno)s - %(levelname)s - %(message)s")
        conlog.setFormatter(formatter)
        logger.addHandler(conlog)
        return logger

    def getFileInfo(self, file):
        fileInfo = {}
        FILE_STAT = os.stat(file)
        fileInfo['file_name'] = os.path.basename(file)
        fileInfo['file_size'] = os.path.getsize(file)
        fileInfo['modified_date'] = datetime.fromtimestamp(int(FILE_STAT.st_mtime))
        fileInfo['create_date'] = datetime.fromtimestamp(int(FILE_STAT.st_ctime))
        fileInfo['file_ext'] = os.path.splitext(file)[1]
        return fileInfo

    def isNeedBkp(self, fileInfo):#www.iplaypython.com
        today = datetime.today()
        if (fileInfo['file_ext'] not in self.exculdeFileExt) and (today - fileInfo['modified_date'] > timedelta(days = 1)):
            return True

    def createBkpFolder(self):
        if not os.path.exists(self.backupFolder):
            os.mkdir(self.backupFolder)


    def bkpFiles(self):
        files = os.listdir(os.getcwd())
        tf = handled = 0
        for i, file in enumerate(files):
            tf = i
            fileInfo = self.getFileInfo(file)
            if self.isNeedBkp(fileInfo):
                self.createBkpFolder()
                self.logger.debug('Moving file {0} to backup folder {1}'.format(fileInfo['file_name'], self.backupFolder))
                shutil.move(file, self.backupFolder)
                handled += 1
        self.logger.debug('Total {} files found, moved {} files to backup folder'.format(tf, handled))

os.chdir('C:/Documents and Settings/**/Desktop')
bkp = BkpDT()
bkp.bkpFiles()

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/text-file/tf2412.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-12 15:16 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部