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程式碼略去,檔案以二進位的模式讀取。
##補充 | |
---|---|
只讀,不可被修改 | |
讀寫,遊標在前,從第一個位置開始修改,印出修改後的字元數 | |
以 | byte方式讀取
|
只寫,若寫入的目標檔案沒有,則創建,有則清空再寫入 | |
轉換為 | byte類型寫入
|
打開文件,把遊標移到有文字的後面,再進行追加 | |
以 | byet類型加入
|
#:r
有兩個表現,一個是讀寫
,還有一個是寫讀
。需要注意。另,w
和a
我沒有寫,是因為學習階段用的相對來說較少##讀取功能
我先以表格形式展示,再進行詳解
I | ),讀前i個字元
| #seek()
#tell() | |
seek() | 前
| readline()
#readlines() | |
\n |
| #truncate()
例如有一個名為 |
##456926667
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中文網其他相關文章!

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

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

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

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

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

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

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具