解决python脚本异常/usr/bin/python3^M: bad interpreter: No such file or directory

问题描述

  • 编写python代码
#!/usr/bin/python3

import fileinput
'''
    ls | ./filein.py
    ./filein.py /etc/passwd
    ./filein.py < /etc/passwd
'''
with fileinput.input() as f_input:
    for line in f_input:
        print(line, end='')
  • 运行python代码
ls | ./filein.py

异常信息

-bash: ./filein.py: /usr/bin/python3^M: bad interpreter: No such file or directory

分析原因

  • windows的换行符和Linux换行符不同

解决方法

  • 在Linux系统上,编写代码即可
vi fileinput
  • 编写python代码
#!/usr/bin/python3

import fileinput
'''
    ls | ./filein.py
    ./filein.py /etc/passwd
    ./filein.py < /etc/passwd
'''
with fileinput.input() as f_input:
    for line in f_input:
        print(line, end='')