搜尋
首頁後端開發Python教學Python是自學檔案操作

Python是自學檔案操作

Dec 16, 2020 pm 05:44 PM
python文件操作

python影片教學專欄介紹自學檔案操作

Python是自學檔案操作

推薦(免費):python影片教學

我是一個學習Python初學者,近期剛學完文件操作。特地分享
文章分為兩部分,第一部分為檔案讀取類型以及讀取的方式,第二部分為練習題

#檔案讀取類型

##檔案讀取的操作方式有以下幾種:唯讀,只寫,追加,讀寫,寫讀

#只讀   r

f = open('test',mode='r',encoding='utf-8')  # 打开文件,读取方式为`r`,编码为UTF-8
f1 = f.read() # 读取文件
print(f1) #打印文件
f.close() # 关闭文件
只讀類型下,檔案不能被修改

讀寫r

f = open('test',mode='rb',encoding='utf-8') # 读取方式变为`r+`
file = f.read()
f1 = f.read()
f.close()
當讀取方式為

r 時,檔案可以被寫入,但是列印出來的是寫入前所讀取的

二進位讀rb程式碼略去,檔案以二進位的模式讀取。

剩下的我將以表格形式統一展現,可以參考對比以上程式碼

讀取方式#rr rbbytewwb byteaabbyet
##補充
只讀,不可被修改
讀寫,遊標在前,從第一個位置開始修改,印出修改後的字元數
方式讀取
只寫,若寫入的目標檔案沒有,則創建,有則清空再寫入
轉換為類型寫入
打開文件,把遊標移到有文字的後面,再進行追加
類型加入

#:r 有兩個表現,一個是讀寫,還有一個是寫讀。需要注意。另,w a 我沒有寫,是因為學習階段用的相對來說較少##讀取功能

我先以表格形式展示,再進行詳解

讀取方式用於場合read()以字元讀,可新增參數(),讀前#seek()調整遊標位置#tell()調整遊標位置,需放置在前readline()前#readlines()將每一行當成列表中元素讀取,換行自帶#truncate()##截取一段讀出來,從後往前讀例如有一個名為test的文件,文件內容如下:
Ii個字元
seek()
\n

##456926667

這是一個長為10個字元的字串
f = open('test',mode='r',encoding='utf-8')  # 打开文件,读取方式为`r`,编码为UTF-8
f1 = f.read(5) # 读取文件中前5个字符
print(f1) #打印文件
f.close() # 关闭文件

列印結果為

45692

f = open('test',mode='w',encoding='utf-8')  # 打开文件,读取方式为`w`,编码为UTF-8
f1 = f.seek()
print(f1) #打印第五个字符
f.close() # 关闭文件
列印結果為

2

剩下的我就不一一做演示,有需要可以自己試試

文件讀取方式

f = open('test',mode='w',encoding='utf-8')
此文件讀取方式,僅能讀取一個文件,另外代碼多

with open('test',mode='w',encoding='utf-8') as f:
      pass
此文件讀取方式,能同時讀取多個文件,且程式碼量相對來說少當需要同時操作兩個或兩個檔案以上時,

with open

方法會相對來說簡易許多

以上是Python是自學檔案操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:简书。如有侵權,請聯絡admin@php.cn刪除
可以在Python數組中存儲哪些數據類型?可以在Python數組中存儲哪些數據類型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

您應該檢查腳本是否使用錯誤的Python版本執行?您應該檢查腳本是否使用錯誤的Python版本執行?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具