Вот сценарий Python, который я написал. Он возвращается из заданного каталога, заменяя все окончания \ n строк окончаниями \ r \ n. Используйте это так:
unix2windows /path/to/some/directory
Он игнорирует файлы в папках, начинающихся с '.'. Он также игнорирует файлы, которые он считает двоичными, используя подход, предложенный Дж. Ф. Себастьяном в этот ответ . Вы можете отфильтровать дальше, используя необязательный позиционный аргумент регулярного выражения:
unix2windows /path/to/some/directory .py$
Вот сценарий полностью. Во избежание сомнений, мои детали лицензированы по лицензии MIT .
#!/usr/bin/python
import sys
import os
import re
from os.path import join
textchars = bytearray({7,8,9,10,12,13,27} | set(range(0x20, 0x100)) - {0x7f})
def is_binary_string(bytes):
return bool(bytes.translate(None, textchars))
def is_binary_file(path):
with open(path, 'rb') as f:
return is_binary_string(f.read(1024))
def convert_file(path):
if not is_binary_file(path):
with open(path, 'r') as f:
text = f.read()
print path
with open(path, 'wb') as f:
f.write(text.replace('\r', '').replace('\n', '\r\n'))
def convert_dir(root_path, pattern):
for root, dirs, files in os.walk(root_path):
for filename in files:
if pattern.search(filename):
path = join(root, filename)
convert_file(path)
# Don't walk hidden dirs
for dir in list(dirs):
if dir[0] == '.':
dirs.remove(dir)
args = sys.argv
if len(args) <= 1 or len(args) > 3:
print "This tool recursively converts files from Unix line endings to"
print "Windows line endings"
print ""
print "USAGE: unix2windows.py PATH [REGEX]"
print "Path: The directory to begin recursively searching from"
print "Regex (optional): Only files matching this regex will be modified"
print ""
else:
root_path = sys.argv[1]
if len(args) == 3:
pattern = sys.argv[2]
else:
pattern = r"."
convert_dir(root_path, re.compile(pattern))