用Python實(shí)現(xiàn)換行符轉(zhuǎn)換的腳本的教程
很簡(jiǎn)單的一個(gè)東西,在'\n'、'\r\n'、'\r'3中換行符之間進(jìn)行轉(zhuǎn)換。
用法
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()的一個(gè)簡(jiǎn)單的練習(xí)??梢杂?,但還相當(dāng)不完善。
#!/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
相關(guān)文章
在Linux系統(tǒng)上通過(guò)uWSGI配置Nginx+Python環(huán)境的教程
這篇文章主要介紹了在Linux系統(tǒng)上通過(guò)uWSGI配置Nginx+Python環(huán)境的教程,示例中為Ubuntu系統(tǒng)并且默認(rèn)使用系統(tǒng)中自帶的Python解釋器,需要的朋友可以參考下2015-12-12
一篇文章教你用python畫(huà)動(dòng)態(tài)愛(ài)心表白
這篇文章主要給大家介紹了關(guān)于如何用python畫(huà)動(dòng)態(tài)愛(ài)心表白的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
在flask中使用python-dotenv+flask-cli自定義命令(推薦)
這篇文章主要介紹了在flask中使用python-dotenv+flask-cli自定義命令的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
pytorch實(shí)現(xiàn)focal loss的兩種方式小結(jié)
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)focal loss的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

