Python實(shí)現(xiàn)文件操作幫助類的示例代碼
一、業(yè)務(wù)需求
在使用Python進(jìn)行業(yè)務(wù)開發(fā)的時(shí)候,需要將一些數(shù)據(jù)保存到本地文件存儲(chǔ),方便后面進(jìn)行數(shù)據(jù)分析展示。
二、需求分析
通過查看需求可得出:需要將數(shù)據(jù)存儲(chǔ)為本地文件(這就是涉及到文件的操作),文件操作屬于基礎(chǔ)內(nèi)容,可以直接將常用的文件操作封裝為一個(gè)文件,后面使用直接調(diào)用即可。
三、實(shí)現(xiàn)方法
3.1、Python文件幫助類
#文件操作
import pickle
#讀取文件的所有內(nèi)容(返回字符串)
def ReadFileAllInfoAsStr(filePathAndName):
try:
with open(filePathAndName) as fileObj:
fileInfos=fileObj.read()
except FileNotFoundError:
msg="很抱歉,文件【"+filePathAndName+"】不存在"
print(msg)
else:
return fileInfos
#讀取文件的所有內(nèi)容(返回列表)
def ReadFileAllInfoAsList(filePathAndName):
try:
with open(filePathAndName) as fileObj:
fileInfos=fileObj.readlines()
except FileNotFoundError:
msg="很抱歉,文件【"+filePathAndName+"】不存在"
print(msg)
else:
return fileInfos
#寫入信息到文件(覆蓋原有內(nèi)容)
def WriteInfo(needWriteInfo,filePathAndName):
try:
with open(filePathAndName,'wb') as fileObj:
tmpBytes = bytes(needWriteInfo,'utf8')
fileObj.write(tmpBytes)
except Exception as e:
print(e)
#追加信息到文件中
def AppedInfo(needWriteInfo,filePathAndName):
try:
with open(filePathAndName,'ab') as fileObj:
tmpBytes = bytes('\n'+needWriteInfo,'utf8')
fileObj.write(tmpBytes)
except Exception as e:
print(e)
#寫入對(duì)象到文件
def WriteObj(needWriteInfo,filePathAndName):
try:
with open(filePathAndName,'wb') as fileObj:
pickle.dump(needWriteInfo,fileObj)
except Exception as e:
print(e)
#讀取文件內(nèi)容
def ReadObj(filePathAndName):
try:
with open(filePathAndName,'rb') as fileObj:
tmpObj = pickle.load(fileObj)
except Exception as e:
print(e)
else:
return tmpObj
import json
import codecs
#寫入信息為json文件
def WritInfoAsJson(needWriteInfo,filePathAndName):
try:
with codecs.open(filePathAndName,'wb',encoding='utf-8') as fileObj:
json.dump(needWriteInfo,fileObj)
except Exception as e:
print(e)
#讀取json文件信息
def ReadInfoToJson(filePathAndName):
try:
with codecs.open(filePathAndName,'rb',encoding='utf-8') as fileObj:
tmpJson=json.load(fileObj)
except Exception as e:
print(e)
else:
return tmpJson
3.2、Python文件幫助類的使用示例
import FileOPC
print('\n寫入信息到文件中')
filePathAndName2='file/test.txt'
tmpstr="測(cè)試寫入內(nèi)容abcdefg"
FileOPC.WriteInfo(tmpstr,filePathAndName2)
print('\n將字符串轉(zhuǎn)為字節(jié)1')
tmpbytes1=str.encode('測(cè)試寫入內(nèi)容','utf-8')
print(tmpbytes1)
print('\n將字符串轉(zhuǎn)為字節(jié)2')
tmpbytes2=bytes('測(cè)試寫入內(nèi)容','utf-8')
print(tmpbytes2)
print('\n追加信息到文件中')
FileOPC.AppedInfo('追加信息123',filePathAndName2)
FileOPC.AppedInfo('測(cè)試追加信息456',filePathAndName2)
print('\n切分字符串')
splitStr="Alice in wonderlan 切割字符串,1,2,3,45,6"
tmpSplit = splitStr.split(',')
print(tmpSplit)
print('\n寫入對(duì)象信息到文件')
filePathAndName3='file/test2.txt'
FileOPC.WriteObj('測(cè)試寫入對(duì)象信息112254799abcadshofdsaujfoduasfoj',filePathAndName3)
print('\n讀取文件對(duì)象')
tmpObj = FileOPC.ReadObj(filePathAndName3)
print(tmpObj)
import json
print('\n寫入信息保存為Json文件')
filePathAndName4='file/testJson.json'
jsonDatas={"101001":[1,3,5,7,9],"101009":["張三","李四",'王五']}
#jsonDatas=[2,3,5,7,11,13]
FileOPC.WritInfoAsJson(jsonDatas,filePathAndName4)
print('\n讀取Json文件信息')
tmpJson=FileOPC.ReadInfoToJson(filePathAndName4)
print(tmpJson)
3.3、示例執(zhí)行結(jié)果


到此這篇關(guān)于Python實(shí)現(xiàn)文件操作幫助類的示例代碼的文章就介紹到這了,更多相關(guān)Python文件操作幫助類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3 pip3 list 出現(xiàn) DEPRECATION 警告的解決方法
今天小編就為大家分享一篇Python3 pip3 list 出現(xiàn) DEPRECATION 警告的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
Python讀入mnist二進(jìn)制圖像文件并顯示實(shí)例
這篇文章主要介紹了Python讀入mnist二進(jìn)制圖像文件并顯示實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python實(shí)現(xiàn)基于兩張圖片生成圓角圖標(biāo)效果的方法
這篇文章主要介紹了python實(shí)現(xiàn)基于兩張圖片生成圓角圖標(biāo)效果的方法,實(shí)例分析了Python使用pil模塊進(jìn)行圖片處理的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
基于Python制作一個(gè)端午節(jié)相關(guān)的小游戲
端午節(jié)快樂,今天我將為大家?guī)硪黄嘘P(guān)端午節(jié)的編程文章,希望能夠?yàn)榇蠹耀I(xiàn)上一份小小的驚喜,我們將會(huì)使用Python來實(shí)現(xiàn)一個(gè)與端午粽子相關(guān)的小應(yīng)用程序,在本文中,我將會(huì)介紹如何用Python代碼制做一個(gè)“粽子拆解器”,感興趣的小伙伴歡迎閱讀2023-06-06
Python selenium實(shí)現(xiàn)斷言3種方法解析
這篇文章主要介紹了Python selenium實(shí)現(xiàn)斷言3種方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

