How can I detect DOS line breaks in a file?

前端 未结 7 635
半阙折子戏
半阙折子戏 2020-12-29 09:46

I have a bunch of files. Some are Unix line endings, many are DOS. I\'d like to test each file to see if if is dos formatted, before I switch the line endings.

How

7条回答
  •  自闭症患者
    2020-12-29 10:18

    As a complete Python newbie & just for fun, I tried to find some minimalistic way of checking this for one file. This seems to work:

    if "\r\n" in open("/path/file.txt","rb").read():
        print "DOS line endings found"
    

    Edit: simplified as per John Machin's comment (no need to use regular expressions).

提交回复
热议问题