激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 用Python實現換行符轉換的腳本的教程

用Python實現換行符轉換的腳本的教程

2020-06-07 10:39dbzhang800 Python

這篇文章主要介紹了用Python實現換行符轉換的腳本的教程,代碼非常簡單,包括一個對操作說明的功能的實現,需要的朋友可以參考下

很簡單的一個東西,在'\n'、'\r\n'、'\r'3中換行符之間進行轉換。
用法

 

復制代碼 代碼如下:

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

 

源碼

這只能算是argparse模塊和os模塊的utime()、stat()、walk()的一個簡單的練習。可以用,但還相當不完善。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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

 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 成人毛片视频在线观看 | 亚洲一区二区中文 | 国产精品久久久久久久亚洲按摩 | freexxxx性女hd性吃奶 | 九九视频精品在线观看 | www.91pron| 性欧美性欧美 | 三级18视频 | 性日本xxx | 中国老女人一级毛片视频 | 毛片免费视频 | 欧美一级黑人 | 日韩黄色片免费看 | 国产成人在线观看免费 | 视频一区 中文字幕 | 高清在线国产 | 一区二区美女视频 | 日本欧美一区二区三区在线观看 | 久久精品性视频 | 精品无码久久久久久国产 | 青草av.久久免费一区 | 九九黄色 | 黄色网址在线播放 | 激情网站免费观看 | 黄色毛片视频在线观看 | 日韩精品免费一区二区三区 | 欧美亚洲国产成人 | 亚洲欧美国产高清 | 亚洲热线99精品视频 | 91重口视频 | 日本精品一区二区 | 黄色7777| 亚洲精品一区二区三区在线看 | 久久久久国| 欧美五月婷婷 | 久久不射电影网 | h视频免费看 | 在线一级片 | 亚洲精品午夜国产va久久成人 | 国色天香综合网 | 国产精品啪一品二区三区粉嫩 |