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)文章
python web框架中實(shí)現(xiàn)原生分頁
這篇文章主要為大家詳細(xì)介紹了python web框架中使用原生分頁的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨(dú)變量的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨(dú)變量的方法,結(jié)合實(shí)例形式分析了Python序列賦值實(shí)現(xiàn)的分解成單獨(dú)變量功能相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Python圖像運(yùn)算之圖像點(diǎn)運(yùn)算與灰度化處理詳解
這篇文章主要介紹了圖像點(diǎn)運(yùn)算的灰度化處理的相關(guān)知識(shí),包括各種灰度算法的實(shí)現(xiàn),以及灰度線性變換和灰度非線性變換。需要的可以參考一下2022-02-02
python中enumerate函數(shù)用法實(shí)例分析
這篇文章主要介紹了python中enumerate函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了enumerate函數(shù)的功能、定義及使用技巧,需要的朋友可以參考下2015-05-05
pandas對(duì)齊運(yùn)算的實(shí)現(xiàn)示例
本文主要介紹了pandas對(duì)齊運(yùn)算的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
信號(hào)生成及DFT的python實(shí)現(xiàn)方式
今天小編就為大家分享一篇信號(hào)生成及DFT的python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02

