Last active
July 23, 2018 13:12
-
-
Save pypeach/0af41a81e43c2b8f71a968b157d8364d to your computer and use it in GitHub Desktop.
ファイルユーティリティ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding:utf-8 | |
""" | |
ファイルアクセスを制御する | |
""" | |
import chardet | |
__author__ = "t.ebinuma" | |
__version__ = "1.0" | |
__date__ = "19 March 2018" | |
def get_file_encoding(file_path): | |
""" | |
ファイルの文字コードを取得する | |
""" | |
# ファイルの文字コードを取得する | |
with open(file_path, mode='rb') as f: | |
fb = f.read() | |
charset = chardet.detect(fb) | |
# UTF-8(BOM含む)の場合は文字コードを返す | |
if charset['encoding'] == 'utf-8' or charset['encoding'] == 'UTF-8-SIG': | |
return charset['encoding'] | |
# utf-8以外はNoneで返す | |
# (ShiftJISの場合はWindows-1254やISO-8859-2を優先する挙動になる) | |
return None | |
def is_exists_str(file_path, find_str): | |
""" | |
ファイル内に指定された文字が含まれているかをチェックする | |
""" | |
# ファイルを読み込む | |
with open(file_path, mode='r', encoding='UTF-8') as f: | |
lines = f.readlines() | |
for line in lines: | |
if line.find(find_str) >= 0: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
loggingに変更