How can I detect DOS line breaks in a file?

前端 未结 7 632
半阙折子戏
半阙折子戏 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 10:25

    (Python 2 only:) If you just want to read text files, either DOS or Unix-formatted, this works:

    print open('myfile.txt', 'U').read()
    

    That is, Python's "universal" file reader will automatically use all the different end of line markers, translating them to "\n".

    http://docs.python.org/library/functions.html#open

    (Thanks handle!)

提交回复
热议问题