Python converts carriage return \ r \ n to \ n, python
Recently in the concise configuration of cocos2d-x, found some friends of the text editor, automatically cut \ r \ n into \ n, (on unix line feed using \ n, windows, the newline uses \ r \ n). Therefore, I wrote this script to help some friends. I don't need to change it in one line.
Import osdef replace (filePath, w2u): try: oldfile = open (filePath, "rb +") # use B to open path, name = OS. path. split (filePath) newfile = open (path + '$' + name, "ba +") old = B 'new = B' if w2u = True: old = B '\ R' new = B' else: old = B '\ n' new = B' \ r \ n' data = B 'while (True ): data = oldfile. read (200) newData = data. replace (old, new) newfile. write (newData) if len (data) <200: break newfile. close () oldfile. close () OS. remove (filePath) OS. rename (path + '$' + name, filePath) failed t IOError as e: print (e) if _ name _ = "_ main __": print ("Enter the file path:") filePath = input () replace (filePath, False) # If this parameter is set to True, \ n can be changed to \ r \ n.
Note that in python, symbols such as \ r \ n cannot be found if the text is opened, only '\ n' can be found, so it must be enabled in B (Binary) mode.