crlf.py 632 Bytes
Newer Older
1
#! /usr/bin/env python3
2 3
"Replace CRLF with LF in argument files.  Print names of changed files."

4
import sys, os
5 6 7 8

def main():
    for filename in sys.argv[1:]:
        if os.path.isdir(filename):
9
            print(filename, "Directory!")
10
            continue
11 12 13
        with open(filename, "rb") as f:
            data = f.read()
        if b'\0' in data:
14
            print(filename, "Binary!")
15
            continue
16
        newdata = data.replace(b"\r\n", b"\n")
17
        if newdata != data:
18
            print(filename)
19 20
            with open(filename, "wb") as f:
                f.write(newdata)
21 22 23

if __name__ == '__main__':
    main()