验证中...
Languages: Python
Categories: Web开发技术
Latest update 2019-07-13 00:33
convert.py
Raw Copy
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
import getopt
import sys
import os
import chardet
import re
ignore_list = [
r'\.git',
r'\.gitignore',
r'.*\.bin$',
]
def in_ignore(s):
for ig in ignore_list:
if re.search(ig, s):
return True
return False
src_file = ""
dest_encode = 'UTF-8'
def help_info():
print('usage: python "convert text encoding.py" -s src_file')
def detect_text_encoding(file_path):
raw_data = open(file_path, 'rb').read()
rs = chardet.detect(raw_data)
return rs['encoding']
# def convert_line_ending(content):
# if '\r\n' in content:
# content.replace('\r\n', '\n')
# if '\r' in content:
# content.replace('\r', '\n')
# return content
def convert_text_encoding(file_path, dest_path, from_encode, to_encode):
if not dest_path:
dest_path = file_path
f_handle1 = open(file_path, 'r', encoding=from_encode)
content = f_handle1.read()
f_handle1.close()
f_handle2 = open(dest_path, 'w', encoding=to_encode)
# content = convert_line_ending(content)
f_handle2.write(content)
f_handle2.close()
# get argments or options from cli
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:d:", ["src=", "dest="])
except getopt.GetoptError as e:
help_info()
sys.exit(1)
for k, v in opts:
if k == "-h":
help_info()
if k == "-s" or k == "--src":
src_file = v
if src_file.strip() == '':
help_info()
sys.exit(1)
if not os.path.exists(src_file):
print("source file do not exists!")
sys.exit(1)
if os.path.isdir(src_file):
for root, dirs, files in os.walk(src_file):
if in_ignore(root):
print('ignore ' + root)
continue
for tmp_f in files:
_tmp_path = os.path.join(root, tmp_f)
if in_ignore(tmp_f):
print('ignore ' + tmp_f)
continue
print(_tmp_path)
_encode = detect_text_encoding(_tmp_path)
# if _encode != dest_encode:
convert_text_encoding(_tmp_path, _tmp_path, _encode, dest_encode)
else:
_encode = detect_text_encoding(src_file)
# if _encode != dest_encode:
convert_text_encoding(src_file, src_file, _encode, dest_encode)

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025