递归扫描指定目录中包含Windows换行符的文件

find_crlf_files.py
使用方式:python3 find_crlf_files.py mydir/

import os
import sys


def find_crlf_files(folder):
    """查找文件夹中所有使用CRLF换行符的文件"""
    count = 0
    for root, dirs, files in os.walk(folder):
        for file in files:
            filepath = os.path.join(root, file)
            try:
                with open(filepath, "rb") as f:
                    if b"\r\n" in f.read():
                        count += 1
                        print(f"[{count}] {filepath}")
            except:
                pass

    print(f"\n共找到 {count} 个使用CRLF换行符的文件")


if __name__ == "__main__":
    folder = "." if len(sys.argv) < 2 else sys.argv[1]
    find_crlf_files(folder)

posted @ 2026-02-05 16:01  wanghongwei-dev  阅读(3)  评论(0)    收藏  举报