40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
修复文件的换行符,将 Windows 风格的 \r\n 转换为 Unix 风格的 \n
|
||
用法:python fix_newlines.py <file1> <file2> ...
|
||
"""
|
||
|
||
import sys
|
||
import os
|
||
|
||
def fix_newlines(file_path):
|
||
"""修复文件的换行符"""
|
||
print(f"修复文件: {file_path}")
|
||
|
||
try:
|
||
# 读取文件内容
|
||
with open(file_path, 'r', encoding='utf-8') as f:
|
||
content = f.read()
|
||
|
||
# 写入文件,使用 Unix 风格的换行符
|
||
with open(file_path, 'w', encoding='utf-8', newline='\n') as f:
|
||
f.write(content)
|
||
|
||
print(f"✅ 修复成功: {file_path}")
|
||
return True
|
||
except Exception as e:
|
||
print(f"❌ 修复失败: {file_path}, 错误: {e}")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
if len(sys.argv) < 2:
|
||
print("用法: python fix_newlines.py <file1> <file2> ...")
|
||
sys.exit(1)
|
||
|
||
# 修复所有指定的文件
|
||
for file_path in sys.argv[1:]:
|
||
if os.path.exists(file_path):
|
||
fix_newlines(file_path)
|
||
else:
|
||
print(f"❌ 文件不存在: {file_path}")
|