[英]env: python\r: No such file or directory
我的 Python 腳本beak
包含以下 shebang:
#!/usr/bin/env python
當我運行腳本$./beak
時,我得到
env: python\r: No such file or directory
我之前從存儲庫中提取了這個腳本。 這可能是什么原因?
在vim
或vi
打開文件,並管理以下命令:
:set ff=unix
保存並退出:
:wq
完畢!
ff
代表文件格式,並且可以接受unix
( \\n
)、 dos
( \\r\\n
) 和mac
( \\r
) 的值(僅用於 pre-intel macs,在現代 macs 上使用unix
) 。
要閱讀有關ff
命令的更多信息:
:help ff
:wq
代表W¯¯儀式和Q UIT,更快的相當於是Shift + ZZ(即按住Shift鍵,然后按z
兩次)。
這兩個命令都必須在命令模式下使用。
沒有必要在 vim 中實際打開文件。 可以直接從命令行進行修改:
vi +':wq ++ff=unix' file_with_dos_linebreaks.py
處理多個*.py
文件(在bash
):
for file in *.py ; do
vi +':w ++ff=unix' +':q' "${file}"
done
😱 offtopic : 如果你碰巧卡在 vim 中並且需要退出,這里有一些簡單的方法。
有時即使在設置了 unix 行結束符之后,您仍然可能會在運行文件時遇到錯誤,特別是如果文件是可執行的並且有shebang 。 該腳本可能有一個 BOM 標記(例如0xEFBBBF
或其他),這會使 shebang 無效並導致 shell 抱怨。 在這些情況下, python myscript.py
可以正常工作(因為 python 可以處理 BOM)但是./myscript.py
即使設置了執行位也會失敗,因為您的 shell(sh、bash、zsh 等)無法處理 BOM標記。 (通常是 Windows 編輯器,例如記事本,它們會創建帶有 BOM 標記的文件。)
可以使用vim
使用以下命令刪除 BOM:
:set nobomb
該腳本包含 CR 字符。 Shell 將這些 CR 字符解釋為參數。
解決方案:使用以下腳本從腳本中刪除 CR 字符。
with open('beak', 'rb+') as f:
content = f.read()
f.seek(0)
f.write(content.replace(b'\r', b''))
f.truncate()
您可以將行結尾轉換為 *nix-friendly 的
dos2unix beak
falsetru 的答案確實解決了我的問題。 我寫了一個小助手,它允許我規范化多個文件的行尾。 由於我不太熟悉多個平台上的行尾內容等,程序中使用的術語可能不是 100% 正確。
#!/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()
我通過運行 python3 解決了這個錯誤,即 python3 \\path\\filename.py
預提交鈎子解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.