#!/usr/bin/env python
# Copyright (c) 2013 Niklas Rosenstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import sys
import glob
import argparse
def process_file(name, lend):
with open(name, 'rb') as fl:
data = fl.read()
data = data.replace('\r\n', '\n').replace('\r', '\n')
data = data.replace('\n', lend)
with open(name, 'wb') as fl:
fl.write(data)
def main():
parser = argparse.ArgumentParser(description='Convert line-endings of one '
'or more files.')
parser.add_argument('-r', '--recursive', action='store_true',
help='Process all files in a given directory recursively.')
parser.add_argument('-d', '--dest', default='unix',
choices=('unix', 'windows'), help='The destination line-ending '
'type. Default is unix.')
parser.add_argument('-e', '--is-expr', action='store_true',
help='Arguments passed for the FILE parameter are treated as '
'glob expressions.')
parser.add_argument('-x', '--dont-issue', help='Do not issue missing files.',
action='store_true')
parser.add_argument('files', metavar='FILE', nargs='*',
help='The files or directories to process.')
args = parser.parse_args()
# Determine the new line-ending.
if args.dest == 'unix':
lend = '\n'
else:
lend = '\r\n'
# Process the files/direcories.
if not args.is_expr:
for name in args.files:
if os.path.isfile(name):
process_file(name, lend)
elif os.path.isdir(name) and args.recursive:
for dirpath, dirnames, files in os.walk(name):
for fn in files:
fn = os.path.join(dirpath, fn)
process_file(fn, fn)
elif not args.dont_issue:
parser.error("File '%s' does not exist." % name)
else:
if not args.recursive:
for name in args.files:
for fn in glob.iglob(name):
process_file(fn, lend)
else:
for name in args.files:
for dirpath, dirnames, files in os.walk('.'):
for fn in glob.iglob(os.path.join(dirpath, name)):
process_file(fn, lend)
if __name__ == "__main__":
main()
6条答案
按热度按时间iezvtpos1#
打开**
vim
或vi
**中的文件,然后执行以下命令:保存并退出:
搞定了!
说明
ff
代表 * file format,可以接受unix
(\n
)、dos
(\r\n
)和mac
(\r
) 的值(仅适用于英特尔之前的MAC,在现代MAC上使用**unix
**)*。要了解有关
ff
命令的更多信息,请执行以下操作::wq
代表W输入和Q退出,更快的等效方法是Shift+zz(即按住 Shift,然后按z
两次)。在command mode中必须同时使用这两个命令。
用于多个文件
没有必要在vim中打开文件,可以直接从命令行进行修改:
要处理多个
*.py
文件(在bash
中):😱 offtopic:如果你碰巧卡在Vim中,需要退出,here是一些简单的方法。
删除BOM标记
有时,即使在设置了unix行尾之后,运行该文件时仍然可能会出现错误,特别是当文件是可执行文件并且具有shebang时。脚本可能具有BOM标记(例如
0xEFBBBF
或其他),这会使shebang无效并导致shell抱怨。(因为python * 可以 * 处理BOM)但是./myscript.py
在设置执行位时会失败,因为您的shell(sh、bash、zsh等)* 不能 * 处理BOM标记。(通常是Windows编辑器,如 Notepad 创建带有BOM标记的文件。)可通过在
vim
中打开文件并执行以下命令来移除BOM:czq61nw12#
脚本包含CR字符。shell将这些CR字符解释为参数。
解决方案:使用以下脚本从脚本中删除CR字符。
dldeef673#
您可以使用以下命令将行尾转换为 *nix-friendly
vohkndzv4#
如果你使用PyCharm,你可以通过将行分隔符设置为LF来很容易地解决这个问题。
yx2lnoni5#
我通过运行python3修复了这个错误,即python3\path\filename.py
ubbxdtey6#
falsetru的答案完全解决了我的问题。我写了一个小助手,允许我规范化多个文件的行尾。由于我不是很熟悉多个平台上的行尾,等等。程序中使用的术语可能不是100%正确。