黑松山资源网 Design By www.paidiu.com
检测文件夹,拷贝有更新的文件到对应目录 2016.5.19
亲测可用,若有借鉴请修改下文件路径;
学习python小一个月后写的这个功能,属于初学,若有大神路过,求代码优化~
newcopy.py:
检测文件夹中最后修改时间变化的文件,并拷贝复制到相应路径下,拷贝目录会自动检测后输出;测试文件夹路径记得修改;
pyinotify.py:
借用window接口,检测脚本所在目录下文件夹变化(更新、删除、添加等),输出日志到桌面上;
# newcopy.py文件 # -*- coding:UTF-8 -*- import os import os.path import sys import time import datetime import stat import difflib import linecache, shutil # 文件全路径和对应最后修改时间写入到out.txt文档中; def add_log(path): with open('out.txt','w') as f: f.close() for root , dirs, files in os.walk(path): for name in files: temp_path = os.path.join(root,name) file_name = temp_path.replace('C:/Users/Enter/Desktop/', '') file_time = os.stat(temp_path).st_mtime with open('out.txt','a') as f: f.write( ','.join( ['%s' % file_name , '%s\n' % file_time] ) ) f.close() # 注意时间格式转换 #file_time = time.localtime(os.stat(root).st_mtime) #file_time=date.strftime('%Y-%m-%d %H:%M:%S') def if_exist(): # 判断文件out.txt是否存在,不存在则创建 filename = 'out.txt' if os.path.exists(filename): message = 'OK, the "%s" file exists.' else: message = "Sorry, I cannot find the '%s' file..and I create it." a = open('out.txt', 'w') a.close() print message % filename # 判断update文件夹是否存在,不存在则创建 files_name='update' if os.path.exists(files_name): message = 'OK, the "%s" file exists.' else: message = "Sorry, I cannot find the '%s' file.and I create it. " os.mkdir('update') print message % files_name # path 待比较的文件夹路径 # 返回生成的txt(包含更新或者添加的文件路径)的路径 def log_compare(path): # 先确保out.txt存在 if_exist() # 获取out.txt文件内容(文件全路径key和最后修改时间value),生成dict txt = open('out.txt', 'r').readlines() myDic = {} for row in txt: (key, value) = row.split(',') myDic[key] = value print myDic # 创建以时间命名的文件和文件夹 setup_filename = str(datetime.datetime.now().strftime('%Y%m%d%H%M%S')) # 获取当前时间 setup_file_path = '%s%s.txt' %('C:/Users/Enter/Desktop/update/' ,setup_filename) # 生成以当前时间命名的.txt文件,准备写入更新日志 setup_file_dir = '%s%s' %('C:/Users/Enter/Desktop/update/' ,setup_filename) # 生成以当前时间命名的.txt文件夹 #判断key,比较value值是否变化 #原始需要有一个out.txt文件,才能比较value确定是否有更新 #运行程序时,重新遍历一遍文件全路径和最后修改时间 for root , dirs, files in os.walk(path): for name in files: temp_path = os.path.join(root,name) file_name = temp_path.replace('C:/Users/Enter/Desktop/', '') time = os.stat(temp_path).st_mtime # 获取最后修改时间 file_time = '%s\n' % time # 加%s\n是为了与out.txt里值完全对应 if myDic.has_key(file_name) == True: if cmp(myDic[file_name], file_time): # myDic[file_name]旧最后修改时间,file_time新最后修改时间 print (file_name,file_time) # 输出有变化的文件名及其对应的最后修改时间 # 输出以文件时间命名的更新日志,生成路径是update下 with open(setup_file_path,'a') as f: # 有更新的文件,写入更新日志 f.write( '%s\n' % file_name ) f.close() else: print "add",file_name with open(setup_file_path,'a') as f: # 新增的文件,写入更新日志 f.write( '%s\n' % file_name ) f.close() # 返回 当前时间,以时间命名的文件夹路径,更新文件路径 return (setup_filename, setup_file_dir, setup_file_path) # 将src目录中的内容拷贝到dest目录 # 如果dest或者其子目录不存在,先创建 # txt_path为更新日志路径,有更新的文件才拷贝 def copy_directory(src, dest, txt_path): if not os.path.exists(txt_path): print "no file update" return # 读更新日志,获取更新文件的全路径 txt = open(txt_path, 'r').readlines() myDic = {} myDic2 = {} for row in txt: myDic[row] = "1" tempArray = os.path.split(row) key = tempArray[0] myDic2[key] = "1" print "myDic2:", myDic2 print "dict:", myDic # 遍历原始文件夹,得到所有文件的全路径 for root, dirs, files in os.walk(src): for name in files: #print "dirs:",dirs fpath = os.path.join(root, name) newroot = root newroot = newroot.replace(src, dest) # 根据文件绝对路径,创建将要拷贝的路径(相对路径),没有则创建 #print newroot rel_dir = root.replace('C:/Users/Enter/Desktop/', '') if not os.path.exists(newroot) and myDic2.has_key(rel_dir): print "rel_dir:" , rel_dir print newroot os.makedirs(newroot) os.chmod(newroot, stat.S_IWRITE) temp = fpath temp = temp.replace(src, dest) rel_path = fpath.replace('C:/Users/Enter/Desktop/', '') # 将绝对路径改为相对路径,便于遍历对比,挑出要拷贝的文件 rel_path += '\n' if myDic.has_key(rel_path) == True: print "real_path:" , rel_path # os.mkdir(rel_path) shutil.copy(fpath, temp) print "copyfile:", fpath def main(): path_dir = 'C:/Users/Enter/Desktop/acd' path_file = 'C:/Users/Enter/Desktop/out.txt' params = log_compare(path_dir) add_log(path_dir) copy_directory(path_dir, params[1], params[2]) if __name__ == '__main__': main()
#pyinotify.py文件 # -*- coding:UTF-8 -*- import os import win32file import win32con # #检测当前目录下所有文件删除、更新、修改等变化。更新日志输出到桌面。2016.5.23 copy ACTIONS = { 1 : "Created", 2 : "Deleted", 3 : "Updated", 4 : "Renamed from something", 5 : "Renamed to something" } # Thanks to Claudio Grondi for the correct set of numbers FILE_LIST_DIRECTORY = 0x0001 path_to_watch = "." hDir = win32file.CreateFile ( path_to_watch, FILE_LIST_DIRECTORY, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None, win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS, None ) while 1: # # ReadDirectoryChangesW takes a previously-created # handle to a directory, a buffer size for results, # a flag to indicate whether to watch subtrees and # a filter of what changes to notify. # # NB Tim Juchcinski reports that he needed to up # the buffer size to be sure of picking up all # events when a large number of files were # deleted at once. # results = win32file.ReadDirectoryChangesW ( hDir, 1024, True, win32con.FILE_NOTIFY_CHANGE_FILE_NAME | win32con.FILE_NOTIFY_CHANGE_DIR_NAME | win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | win32con.FILE_NOTIFY_CHANGE_SIZE | win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | win32con.FILE_NOTIFY_CHANGE_SECURITY, None, None ) #print "results:", results for action, file in results: full_filename = os.path.join (path_to_watch, file) print full_filename, ACTIONS.get (action, "Unknown") with open('C:/Users/Enter/Desktop/fileupdate.txt','a') as f: #str = ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] ) #print str f.write( ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] ) ) f.close()
以上这篇python检测文件夹变化,并拷贝有更新的文件到对应目录的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月08日
2024年10月08日
- 《丁当 20首重量级歌曲 Fu Good 下一站天后 2CD》[WAV/分轨][650MB]
- 歌莉雅.2012-My.Voice.Story【环星】【FLAC分轨】
- 群星.1993-一曲成名·青春无悔【飞碟】【WAV+CUE】
- 刘德华.2000-爱无知(金装版)【NEWMELODY】【WAV+CUE】
- 许巍《时光漫步》1CD[FLAC/分轨][804.3MB]
- 群星《2024第一季度百度歌曲排行抖音榜top100》1CD[FLAC/分轨][2.3GB]
- 张柏芝《全新经验》[FLAC/分轨][450MB]
- 唐朝.2008-浪漫骑士【音乐家】【WAV+CUE】
- 张芸京.2016-失败的高歌【泡耳音乐】【WAV+CUE】
- 群星.1991-音乐工厂1·皇后大道东【音乐工厂】【WAV+CUE】
- 群星.1992-音乐工厂2·首都【音乐工厂】【WAV+CUE】
- 群星.1994-音乐工厂3·儿童乐园【音乐工厂】【WAV+CUE】
- 胡鸿钧.2021-ex:CHANGE【星梦娱乐】【WAV+CUE】
- 班得瑞原装进口《第一张新世纪专辑:仙境》1CD[APE/CUE分轨][292.3MB]
- 伍佰《摇滚教父 伍佰黄金精选 2CD》[WAV+CUE][990MB]