这篇文章是 另外一个博客的延续 ,这个使用os.listdir写成的
import os
import io
import sys
import shutil
import hashlib
import re
def Usage():
print("Usage:\n\t%s src_dir dst_dir" % (os.path.basename(sys.argv[0])))
sys.exit(4)
def checksrcdir(src_dir):
if not os.path.exists(src_dir):
print("源文件夹: %s 不存在" % src_dir)
sys.exit(3)
return False
def checkdstdir(dst_dir):
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
return True
def computmd5(filename, readsize):
hs = hashlib.md5()
with open(filename, mode='rb') as bigfile:
content = bigfile.read(readsize)
if content:
hs.update(content)
return hs.hexdigest()
def checkfilesize(filename):
filesize = os.path.getsize(filename)
return filesize
def write_log_to_file(filename, messages):
"""
:param filename:
:param messages:
"""
with open(filename, mode='a+', encoding='utf-8') as rg:
rg.writelines(messages)
rg.close()
def truncatefile(filename):
with open(filename, mode='w+', encoding='utf-8') as rg:
rg.truncate()
rg.close()
def copyfile(srcfile, dstfile):
shutil.copy2(src=srcfile, dst=dstfile)
def countnumber(dirs,filename):
count = 0
for _ in os.listdir(dirs):
count += 1
msg = "%s 目录下共有 %d 个文件\n" % (dirs, count)
write_log_to_file(filename,messages=msg)
def scandirictory(dirs):
count = 0
for files_or_dirictories in os.listdir(dirs):
count += 1
src_full_file_path = os.path.join(dirs, files_or_dirictories)
if os.path.isfile(src_full_file_path):
size = checkfilesize(src_full_file_path)
if size < 104857600:
dst_full_file_path = src_full_file_path.replace(src_dir,dst_dir)
srcMd5Value = computmd5(filename=src_full_file_path,readsize=-1)
if os.path.exists(dst_full_file_path):
dstMd5Value = computmd5(filename=dst_full_file_path,readsize=-1)
if not srcMd5Value == dstMd5Value:
msg = "%s --> %s md5不匹配,立即替换 %s。\n\t源文件md5为%s,终文件md5为:%s\n" % (src_full_file_path,dst_full_file_path,dst_full_file_path,
srcMd5Value,dstMd5Value)
copyfile(srcfile=src_full_file_path,dstfile=dst_dir)
write_log_to_file(filename='oslistdir_error.log',messages=msg)
else:
msg = "%s 已存在 %s \n" % (src_full_file_path,dst_full_file_path)
write_log_to_file(filename='oslistdir_exist.log',messages=msg)
else:
copyfile(srcfile=src_full_file_path,dstfile=dst_full_file_path)
dstMd5Value = computmd5(filename=dst_full_file_path, readsize=-1)
if srcMd5Value == dstMd5Value:
msg = "%s 复制到 %s\n\t源文件md5:%s\t终文件md5:%s\n" % (src_full_file_path,dst_full_file_path,srcMd5Value,dstMd5Value)
write_log_to_file(filename='oslistdir_copied.log',messages=msg)
else:
dst_full_file_path = src_full_file_path.replace(src_dir, dst_dir)
srcMd5Value = computmd5(filename=src_full_file_path, readsize=104857600)
if os.path.exists(dst_full_file_path):
dstMd5Value = computmd5(filename=dst_full_file_path, readsize=104857600)
if not srcMd5Value == dstMd5Value:
msg = "%s --> %s md5不匹配,立即替换 %s。\n\t源文件md5为%s,终文件md5为:%s\n" % (
src_full_file_path, dst_full_file_path, dst_full_file_path,
srcMd5Value, dstMd5Value)
copyfile(srcfile=src_full_file_path, dstfile=dst_dir)
write_log_to_file(filename='oslistdir_error.log', messages=msg)
else:
msg = "%s 已存在 %s \n" % (src_full_file_path, dst_full_file_path)
write_log_to_file(filename='oslistdir_exist.log', messages=msg)
else:
copyfile(srcfile=src_full_file_path, dstfile=dst_full_file_path)
dstMd5Value = computmd5(filename=dst_full_file_path, readsize=-1)
if srcMd5Value == dstMd5Value:
msg = "%s 复制到 %s\n\t源文件md5:%s\t终文件md5:%s\n" % (
src_full_file_path, dst_full_file_path, srcMd5Value, dstMd5Value)
write_log_to_file(filename='oslistdir_copied.log', messages=msg)
if os.path.isdir(src_full_file_path):
ds = src_full_file_path.replace(src_dir+'\\',dst_dir+'\\')
if not os.path.exists(ds):
os.makedirs(ds)
scandirictory(src_full_file_path)
countnumber(dirs=ds,filename="dist_oslistdir_number.log")
msg = "%s 有 %d 个文件\n" % (dirs, count)
write_log_to_file(filename='source_oslistdir_number.log',messages=msg)
if __name__ == '__main__':
src_dir = "F:\VirtualMachines\openmanager"
dst_dir = 'F:\Downloads'
checksrcdir(src_dir=src_dir)
checkdstdir(dst_dir=dst_dir)
scandirictory(src_dir)
countnumber(dirs=dst_dir,filename="dist_oslistdir_number.log")