python⾃动复制粘贴_Python⾃动复制⽂件程序⽰例
欢迎,来⾃IP地址为:111.178.86.121 的朋友
Python不仅是⼀门编程语⾔,并且还常常被⽤来完成运维⼯作。⼏句简单的脚本,就可以⾃动完成很多任务。
因为误删⽽丢失的情况经常发⽣,定期备份重要⽂件和⽂件夹是⼀种⾮常有效的⽅案。但是如果⽂件较⼤的话,就需要耗费⼤量时间成本,为了避免每天重复复制粘贴的⽆聊操作,可以使⽤以下Python脚本⾃动完成。
复制脚本可以根据设置的间隔⾃动复制指定⽂件(⽂件夹),并记录相关⽇志信息,⽅便起见将复制脚本命名为”sync.py”。
本⽰例程序包含如下三个⽂件:
Sync.py:主程序,⽤于完成⾃动复制
Sync.ini:配置⽂件
Logger.py:⽇志记录模块
同时,sync.py主程序还会创建⼀个Sync.log⽂件⽤于记录复制⽇志。主程序⽰例如下:
import configparser
import time
import shutil
import hashlib
from distutils.dir_util import copy_tree
from collections import OrderedDict
import os
import Logger
def ConfRead():
config = configparser.ConfigParser()
return (dict(config.items("Para")))
def md5(fname,size=4096):
alwaysonline什么意思hash_md5 = hashlib.md5()
with open(fname,"rb") as f:
for chunk in iter(lambda: f.read(size),b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def CopyDir(fromDir,toDir):
copy_tree(fromDir,toDir)
公务员转正工作总结
def CopyFiles(fromFile,path_to_copy):
def OriginalFiles():蓝洁瑛变疯内幕
drive = ("from")
Files_Dict = OrderedDict()
print(drive)
for root,dir,files in os.walk(drive,topdown=True):
for file in files:
file = file.lower()
file_path = root +'\\'+file
try:
hash_mark = md5(file_path,size=4096)
rel_path = file_path.strip(drive)
Files_Dict[(hash_mark,rel_path)]= file_path
except Exception as e :
('Error Original files: {0}'.format(e))
return Files_Dict
def Destination():
Files_Dict = OrderedDict()
_from = ("from")
_to = ("to")
_dir = _from.rsplit("\\",)[1]
drive = _to+"\\"+_dir
print(drive)追光吧未删光郑爽镜头
try:
if os.path.isdir(drive):
for root,dir,files in os.walk(drive,topdown=True):
for file in files:
file = file.lower()
file_path = root+"\\"+file
try:
hash_mark = md5(file_path,size=4096)
rel_path = file_path.strip(drive)
Files_Dict[(hash_mark,rel_path)]=file_path
except Exception as e:
('Error Destination for loop: {0}'.format(e)) return Files_Dict
CopyDir(_from,drive)
Logger.logger.info('Full folder: {0} copied'.format(_from)) return None
except Exception as e :
('Error Destination: {0}'.format(e))
def LogicCompare():
_from = ("from")
_to = ("to")
Dest_dict = Destination()
if Dest_dict:
Source_dict = OriginalFiles()
remaining_files = set(Source_dict.keys())- set(Dest_dict.keys()) remaining_files = [(k) for k in remaining_files] for file_path in remaining_files:
try:
Logger.logger.info('File: {0}'.format(file_path))
许魏洲演唱会dir, file = file_path.rsplit("\\",1)
rel_dir = _from.rsplit("\\",1)[1]
rel_dir1 = place(_from,"")
dest_dir = _to
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
CopyFiles(file_path, dest_dir)
except Exception as e:
('Error LogicCompare: {0}'.format(e))
if __name__ == '__main__':
All_Config = ConfRead()
Freq = int(("freq"))*60
Total_time = int(("total_time"))*60
repeat = int(Total_time/Freq)
i =1
while True:
if i >= repeat:
让男人看了心疼愧疚的说说LogicCompare()
time.sleep(Freq)
i = i +1
程序⾸先读取配置⽂件,然后根据配置内容浏览源⽂件夹和⽬标⽂件夹,并根据⽂件名进⾏⽐较,最后进⾏复制备份。根据需要,需要⽂件内容如下:
[Para]
From = D:\Test
To = D:\Backup
Freq = 1
Total_time = 5
程序执⾏后就会根据设置的时间间隔和频率进⾏检测和备份,不需要⼈⼯⼲预。⽇志⽂件如下所⽰:
2018-12-25 11:43:03,651-INFO-Full folder: D:\Test copied
2018-12-25 11:43:33,300-INFO-File: D:\Test\test3.bmp
2018-12-25 11:43:33,322-INFO-File: D:\Test\test1.bmp
2018-12-25 11:43:33,347-INFO-File: D:\Test\test2.bmp