1. 程式人生 > >Linux 執行Python指令碼出現:No such file or directory

Linux 執行Python指令碼出現:No such file or directory

寫好了一個Python 指令碼後 準備在Linux上面執行。  使用的是 ./test.py  出現了 :No such file or directory  

而在使用 python  test.py 時卻是可以的。這個非常的讓人費解,於是刷了一下 stack overflow 。原因就是說在指令碼的頭行

#!/usr/bin/env python  中,其實是這樣的#!/usr/bin/env python\r  而\r 會被shell 當成引數,所以可以寫點程式碼來刪除\r

with open('beak', 'rb+') as f:
    content = f.read()
    f.seek(0)
    f.write(content.replace(b'\r', b''))
    f.truncate()


還有更加簡單的方法 就是 vi 的時候  先使用  :set  ff=unix  在 :wq 儲存 執行即可。

解決原文