python 文件讀寫模板

'''
Created on 2016年4月22日

@author: wty
'''
import os


def readFile(filePath, charset):
    with open(filePath, "rb") as fr:
        return fr.read().decode(charset, "replace")

def readFileLines(filePath, charset):
    with open(filePath, "rb") as fr:
        return [line.decode(charset, "replace") for line in fr.readlines()]

def writeFile(filePath, content, charset):
    with open(filePath, "wb") as fw:
        fw.write(content.encode(charset, "replace"))

def readDictionary(dictionaryPath, charset):
    files = os.listdir(dictionaryPath)
    ret = []
    for file in files:
        path = dictionaryPath + os.path.sep + file
        if os.path.isfile(path):
            content = readFile(path, charset)
            ret.append(content)
        else:
            ret.extend(readDictionary(path, charset))
    return ret
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章