python with open 循环建立指定名字文件_Python基础——文件
作者丨文靜
來源丨醫數思維云課堂(ID:Datamedi)? ?
文件(file)
1、 通過python程序來對計算機中的各種文件進行增刪改查的操作。
2、?I/O(Input/Output),文件的輸入輸出。
3、 操作文件的步驟:
(1)打開文件
(2)對文件進行各種操作(讀、寫),然后保存
(3)關閉文件
4、 打開文件:
使用open()函數來打開一個文件,參數:file是要打開的文件的名字(路徑),首先創建一個變量,來保存文件的名字,然后file_name='demo.txt' open(file_name)
5、 返回值:
返回一個對象,這個對象就代表了當前打開的文件。
例如:
file_name='demo.txt'
file_obj=open(file_name)
print(file_obj)#證明文件存在且一打開
6、 如果目標文件和當前文件在同一級目錄下,則直接使用文件名即可。
如果不在同一級目錄下,比如demo.txt,在hello文件夾中,則要file_name='hello/demo.txt'
7、 表示路徑,可以使用..來返回一級目錄
例如:
file_name='../hello/demo.txt'
8、如果目標文件距離當前文件比較遠,比如說文件在桌面,此時可以使用絕對路徑,絕對路徑應該從磁盤的根目錄開始書寫,win+r可彈出搜索窗口-再輸cmd回車調出命令窗口-cd Desktop-輸入dir-粘貼路徑
例如:
file_name=r'C:/Users/茶茶/Desktop/demo.txt'
9、讀取文件:
首先打開桌面的文件
file_name=r'C:/Users/茶茶/Desktop/demo.txt'
file_obj=open(file_name) print(file_obj),然后讀取文件中的內容,當我們獲取了文件對象以后,所有的對文件的操作都應該通過對象來進行。
讀取內容,用read()方法,它會將內容全部保存為一個字符串返回。
例如:
content=file_obj.read()
print(content)
這個程序會自動關閉。
10、關閉文件:
調用close()方法來關閉文件
例如:
file_obj.close()
此時再輸入content=file_obj.read() print(content),就會報錯。
11、with…as語句:
在with語句中可以直接使用file_obj來做文件操作,此時這個文件只能在with中使用,一旦with結束,則文件會自動結束。
這個會簡化一些,不用手動關閉文件了。
例如:
file_name=r'C:/Users/茶茶/Desktop/demo.txt'
with open(file_name) as file_obj:
print(file_obj.read())
12、對異常信息應用,捕獲錯誤信息:
例如:
file_name=r'C:/Users/茶茶/Desktop/hello.txt'
try:
with open(file_name) as file_obj:
print(file_obj.read())
except FileNotFoundError:
print(f'{file_name}文件不存在~~')
13、文件的簡單讀取:
調用open()來打開一個文件,可以將文件分成兩種類型:
一種是純文本文件(使用utf-8等編碼編寫的文件)
另一種是二進制文件(圖片、mp3、ppt等文件)
通過read()來讀取文件中的內容,默認是以文本文件的形式打開的。
通過open()默認的編碼為None,所以處理文本文件時,必須要指定文件的編碼。
中文文件要加個encoding='utf-8'。
14、如果要讀取的文件較大的話,會一次性將文件的內容加載到內存中,導致內存泄漏,所以對于較大的文件,不要直接調用read(),read()可以接收一個size作為參數,該參數用來指定要讀取的字符數量,默認值為-1,它會讀取文件中的所有字符。
可以為size指定一個值,這樣read()會讀取指定數量的字符,每一次讀取都是從上次讀到位置開始讀取的,如果字符的數量小于size,則會讀取剩余所有的,如果已經讀取到了文件的最后,則會返回空串' '。
例如:
file_name=r'C:/Users/茶茶/Desktop/demo2.txt'
with open(file_name) as file_obj:
content=file_obj.read(-1)
print(content)
print(len(content))
讀取前七個字符:
file_name=r'C:/Users/茶茶/Desktop/demo2.txt'
with open(file_name) as file_obj:
content=file_obj.read(7)
print(content)
print(len(content))
繼續再讀七個字符:
file_name=r'C:/Users/茶茶/Desktop/demo2.txt'
with open(file_name) as file_obj:
content=file_obj.read(7)
content=file_obj.read(7)
print(content)
print(len(content))
15、readline()每次讀一行,readlines()會一次性將讀取到的內容封裝到一個列表中返回。
例如:
file_name=r'C:/Users/茶茶/Desktop/demo2.txt'
with open(file_name) as file_obj:
print(file_obj.readline())
print(file_obj.readline())
16、也可以用for循環
例如:
file_name=r'C:/Users/茶茶/Desktop/demo2.txt'
with open(file_name) as file_obj:
for t in file_obj:
print(t)
17、write()來向文件中寫入內容,如果操作的是一個文本文件的話,則write()需要傳遞一個字符串作為參數,使用open()打開文件時必須要指定打開文件時索要做的操作(讀、寫、追加),如果不指定操作類型,則默認是讀取文件。
而讀取文件時,是不能向文件中寫入的,r表示只讀的,w表示是可寫的,使用w來寫入文件時,如果文件不存在會創建文件,如果文件存在則會截斷文件。
截斷文件指刪除原來文件中的所有內容。
該方法可以分多次向文件中寫入內容。
例如:
file_name=r'C:/Users/茶茶Desktop/demo.txt'
with open(file_name,'w') as file_obj:
file_obj.write('hello hello how are you!')
例如:
file_name=r'C:/Users/茶茶/Desktop/demo3.txt'
with open(file_name,'w') as file_obj:
file_obj.write('hello hello how are you!')
例如:
file_name=r'C:/Users/茶茶/Desktop/demo.txt'
with open(file_name,'w') as file_obj:
file_obj.write('hello hello how are you!
')
file_obj.write('hello hello how are you!
')
file_obj.write('hello hello how are you!
')
18、a表示追加內容,如果文件存在,則會向文件中追加內容,r+表示既可讀也可寫,文件不存在會報錯。
例如:
file_name=r'C:/Users/茶茶/Desktop/demo.txt'
with open(file_name,'a') as file_obj:
file_obj.write('hello hello how are you!
')
file_obj.write('hello hello how are you!
')
file_obj.write('hello hello how are you!
')
19、x用來新建文件,如果文件不存在則創建,存在則報錯。
想要了解更多,可以點擊閱讀全文哦。
往期推薦
點擊圖片直達原文
?關注醫數思維云課堂——醫數思維云課堂(ID:Datamedi),每周四20:00準時更新課程,陪60萬醫學生共同成長,轉載請聯系我們授權。
覺得“好看”,請點這里↓↓↓
總結
以上是生活随笔為你收集整理的python with open 循环建立指定名字文件_Python基础——文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: micropython esp32驱动舵
- 下一篇: pythonclass实例化_Pytho