Python line break conversion script tutorial, python line break tutorial

Source: Internet
Author: User

Python line break conversion script tutorial, python line break tutorial

A simple thing is to convert line breaks in '\ n',' \ r \ n', and '\ R' 3.
Usage

Copy codeThe 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)|(?<!\r)\n", "\r\n", temp)     return temp      def convert_file(filename, args):     statinfo = None     with file(filename, 'rb+') as f:       data = f.read()       if '\0' in data and not args.force: #skip binary file... ?         print '%s is a binary file?, skip...' % filename         return       newdata = convert_line_endings(data, args.mode)       if (data != newdata):         statinfo = os.stat(filename) if args.keepdate else None         f.seek(0)         f.write(newdata)         f.truncate()     if statinfo:       os.utime(filename, (statinfo.st_atime, statinfo.st_mtime))     print filename      def walk_dir(d, args):     for root, dirs, files in os.walk(d):       for name in files:         convert_file(os.path.join(root, name), args)      if __name__ == '__main__':     import argparse     import sys     parser = argparse.ArgumentParser(description='Convert Line Ending')     parser.add_argument('filename', nargs='+', help='file names')     parser.add_argument('-r', dest='recursive', action='store_true',         help='walk through directory')     parser.add_argument('-m', dest='mode', default='d', choices='upwmd',         help='mode of the line ending')     parser.add_argument('-k', dest='keepdate', action='store_true',         help='keep output file date')     parser.add_argument('-f', dest='force', action='store_true',         help='force conversion of binary files')     args = parser.parse_args()     if args.mode == 'd':       args.mode = 'w' if sys.platform == 'win32' else 'p'        for filename in args.filename:       if os.path.isdir(filename):         if args.recursive:           walk_dir(filename, args)         else:           print '%s is a directory, skip...' % filename       elif os.path.exists(filename):         convert_file(filename, args)       else:         print '%s does not exist' % filename 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.