This article describes how to use Python to implement the script for line break conversion. the code is very simple, including the implementation of the operation description function. if you need it, you can refer to a very simple one, line breaks in '\ n',' \ r \ n', and '\ r' 3 are converted.
Usage
The code is as follows:
Usage: eol_convert.py [-h] [-r] [-m {u, p, w, m, d}] [-k] [-f]
Filename [filename...]
Convert Line Ending
Positional arguments:
Filename file names
Optional arguments:
-H, -- help show this help message and exit
-R walk through directory
-M {u, p, w, m, d} mode of the line ending
-K keep output file date
-F force conversion of binary files
Source code
This is only a simple exercise for argparse and OS modules, such as utime (), stat (), and walk. It can be used, but it is still quite imperfect.
#!/usr/bin/env python #2009-2011 dbzhang800 import os import re import os.path def convert_line_endings(temp, mode): if mode in ['u', 'p']: #unix, posix temp = temp.replace('\r\n', '\n') temp = temp.replace('\r', '\n') elif mode == 'm': #mac (before Mac OS 9) temp = temp.replace('\r\n', '\r') temp = temp.replace('\n', '\r') elif mode == 'w': #windows temp = re.sub("\r(?!\n)|(?