#!/usr/bin/env python3 # sample-crlf.py: a Python file saved with Windows (CRLF) line endings. # Free sample from 99helpers.com/file-types/py # # python3 sample-crlf.py works: Python accepts CRLF, LF and CR line endings. # ./sample-crlf.py fails on macOS and Linux, because the shebang line # ends in an invisible carriage return and the system # looks for a program called "python3\r": # /usr/bin/env: 'python3\r': No such file or directory # Fix: convert to LF with dos2unix, or your editor's line-ending setting. from pathlib import Path data = Path(__file__).read_bytes() crlf = data.count(b"\r\n") lf_only = data.count(b"\n") - crlf print(f"{crlf} lines end in CRLF and {lf_only} in LF alone.") print("Python ran this file without complaint.")