Python如何實作檔案操作幫助類
一、業務需求
在使用Python進行業務開發的時候,需要將一些數據保存到本地文件存儲,方便後面進行數據分析展示。
二、需求分析
透過查看需求可得出:需要將資料儲存為本機檔案(這就是涉及到檔案的操作),檔案操作屬於基礎內容,可以直接將常用的文件操作封裝為一個文件,後面使用直接呼叫即可。
三、實作方法
3.1、Python檔案幫助類別
#文件操作 import pickle #读取文件的所有内容(返回字符串) def ReadFileAllInfoAsStr(filePathAndName): try: with open(filePathAndName) as fileObj: fileInfos=fileObj.read() except FileNotFoundError: msg="很抱歉,文件【"+filePathAndName+"】不存在" print(msg) else: return fileInfos #读取文件的所有内容(返回列表) def ReadFileAllInfoAsList(filePathAndName): try: with open(filePathAndName) as fileObj: fileInfos=fileObj.readlines() except FileNotFoundError: msg="很抱歉,文件【"+filePathAndName+"】不存在" print(msg) else: return fileInfos #写入信息到文件(覆盖原有内容) 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) #写入对象到文件 def WriteObj(needWriteInfo,filePathAndName): try: with open(filePathAndName,'wb') as fileObj: pickle.dump(needWriteInfo,fileObj) except Exception as e: print(e) #读取文件内容 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="测试写入内容abcdefg" FileOPC.WriteInfo(tmpstr,filePathAndName2) print('\n将字符串转为字节1') tmpbytes1=str.encode('测试写入内容','utf-8') print(tmpbytes1) print('\n将字符串转为字节2') tmpbytes2=bytes('测试写入内容','utf-8') print(tmpbytes2) print('\n追加信息到文件中') FileOPC.AppedInfo('追加信息123',filePathAndName2) FileOPC.AppedInfo('测试追加信息456',filePathAndName2) print('\n切分字符串') splitStr="Alice in wonderlan 切割字符串,1,2,3,45,6" tmpSplit = splitStr.split(',') print(tmpSplit) print('\n写入对象信息到文件') filePathAndName3='file/test2.txt' FileOPC.WriteObj('测试写入对象信息112254799abcadshofdsaujfoduasfoj',filePathAndName3) print('\n读取文件对象') 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、範例執行結果
以上是Python如何實作檔案操作幫助類的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

在 Sublime Text 中運行 Python 代碼,需先安裝 Python 插件,再創建 .py 文件並編寫代碼,最後按 Ctrl B 運行代碼,輸出會在控制台中顯示。

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

VS Code可以在Windows 8上運行,但體驗可能不佳。首先確保系統已更新到最新補丁,然後下載與系統架構匹配的VS Code安裝包,按照提示安裝。安裝後,注意某些擴展程序可能與Windows 8不兼容,需要尋找替代擴展或在虛擬機中使用更新的Windows系統。安裝必要的擴展,檢查是否正常工作。儘管VS Code在Windows 8上可行,但建議升級到更新的Windows系統以獲得更好的開發體驗和安全保障。

在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。
