类似于数据库的基本操作增删改查
,工作中会经常出现使用python完成文件操作。
本文作者实现文件操作相关函数。
def writeTextFile(filePath, fileContent, encoding='utf8'):
with open(filePath, 'w', encoding=encoding) as file:
file.write(fileContent)
import os
def getFileNameList(dirPath, partOfFileName=''):
allFileName_list = list(os.walk(dirPath))[0][2]
fileName_list = [k for k in allFileName_list if partOfFileName in k]
return fileName_list
def getFilePathList(dirPath, partOfFileName=''):
allFileName_list = list(os.walk(dirPath))[0][2]
fileName_list = [k for k in allFileName_list if partOfFileName in k]
filePath_list = [os.path.join(dirPath, k) for k in fileName_list]
return filePath_list
def readTextFile(filePath, encoding='utf8'):
with open(filePath, encoding=encoding) as file:
return file.read()