最近经常从mac往Nas的NFS共享文件夹备份文件,文件量大的时候可能偶尔会出现复制中断现象,所以写了个python脚本递归对比文件夹下文件的数量和大小。
代码是使用CodeCursor的VScode插件写的
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
def main():
src_path = "src_path"
dst_path = "dst_path"
resultfile = "result_of_compare.md"
# Initialize table content
table_content = "| 文件夹名称 | result | 多余的文件 | 缺少的文件 | 文件大小不同 |\n| --- | --- | --- | --- | --- |\n"
# Traverse the src directory and its subdirectories to get all subdirectory names
for root, dirs, files in os.walk(src_path):
dirs.sort() # Sort the dirs list
for subdir in dirs:
# Check if the corresponding subdirectory exists in the dst directory
dst_subdir = os.path.join(dst_path, os.path.relpath(os.path.join(root, subdir), src_path))
if os.path.isdir(dst_subdir):
# If it exists, compare the files in the two subdirectories
result = "OK"
# src_files = set(os.listdir(os.path.join(root, subdir)))
# dst_files = set(os.listdir(dst_subdir))
src_files = set(file for file in os.listdir(os.path.join(root, subdir)) if os.path.isfile(os.path.join(root, subdir, file)))
dst_files = set(file for file in os.listdir(os.path.join(dst_path, os.path.relpath(os.path.join(root, subdir), src_path))) if os.path.isfile(os.path.join(dst_path, os.path.relpath(os.path.join(root, subdir), src_path), file)))
extra_files = dst_files - src_files
missing_files = src_files - dst_files
size_diff_files = set()
for file in src_files & dst_files:
src_file_path = os.path.join(root, subdir, file)
dst_file_path = os.path.join(dst_subdir, file)
if os.path.getsize(src_file_path) != os.path.getsize(dst_file_path):
size_diff_files.add(file)
# Convert the extra, missing, and size different file names to strings
extra_files_str = ", ".join(extra_files) if extra_files else "-"
missing_files_str = ", ".join(missing_files) if missing_files else "-"
size_diff_files_str = ", ".join(size_diff_files) if size_diff_files else "-"
else:
# If it does not exist, set the result to "N/A" and the extra, missing, and size different file names to "-"
result = "N/A"
extra_files_str = "-"
missing_files_str = "-"
size_diff_files_str = "-"
# Add the subdirectory information to the table content
table_content += f"| {os.path.relpath(os.path.join(root, subdir), src_path)} | {result} | {extra_files_str} | {missing_files_str} | {size_diff_files_str} |\n"
# Write the table content to the compare.md file
with open(f"{resultfile}", "w") as f:
f.write(table_content)
os.system(f"code {resultfile}")
if __name__ == '__main__':
main()