Python文件操作
Pythont提供了os罢维、os.path暇昂、shutil等模塊用于處理文件
文件的打開或創(chuàng)建
文件的打開或創(chuàng)建可以使用內(nèi)斂模塊的函數(shù)file()。該函數(shù)可以指定處理模式敬尺,設(shè)置文件為只讀肢执、只寫
或可讀可寫狀態(tài)。file()的聲明如下所示:
<pre>
file(name[,mode[,buffering]])
name:是被打開的文件名惰匙。如果name不存在技掏,file()將創(chuàng)建名為name的文件。再打開
mode:指文件的打開模式项鬼。
buffering:設(shè)置緩存模式哑梳。0表示不緩存,1表示緩存绘盟。如果大于1表示緩沖去的大小
</pre>
參數(shù) | 說明 |
---|---|
r | 以只讀方式打開文件 |
r+ | 以讀寫方式打開文件 |
w | 以寫入的方式打開文件鸠真。 重新寫入。如果文件不存在龄毡,則創(chuàng)建 |
w+ | 以讀寫的方式打開文件吠卷。 重新寫入。如果文件不存在沦零,則創(chuàng)建 |
a | 以寫入的方式打開文件祭隔。追加新內(nèi)容如果文件不存在,則創(chuàng)建 |
a+ | 以讀寫的方式打開文件路操。追加新內(nèi)容如果文件不存在疾渴,則創(chuàng)建 |
b | 以二進制模式打開文件⊥驼蹋可與以上模式結(jié)合使用 |
U | 支持所有的換行符號 |
注:圖片搞坝、視頻的等需要用b模式打開
文件的讀取
<pre>
f = file("hello.txt",'r') #以只讀方式打開文件,此時文件已存在
while True:
line = f.readline() #從文件中讀取一行作為字符串返回 readline([size]). size表示大小
if line:
print(line)
else:
break
f.close() #關(guān)閉文件
print(f.closed) #文件是否關(guān)閉 此時返回True
print(f.mode) #文件的打開模式 r
輸入結(jié)果
hello world
hello China
hello Keke goodbye
True
r
其他函數(shù):
f.readlines() 把文件每行存儲在列表中返回 ['hello world\n', '\n', 'hello China\n', '\n', 'hello Keke goodbye\n']
f.seek(offset[,whence])把文件的指針一道1個新的位置。offset表示響度與whence用于設(shè)置相對位置的起點魁袜,0表示從文件開始計算瞄沙;
1表示從當(dāng)前位置開始計算己沛;2表示從文件末尾開始計算。如果whence省略距境,offset表示相對文件開頭的位置申尼。
f.tell() 返回文件指針當(dāng)前的位置。
f.next() 返回下一行的內(nèi)容垫桂,并將文件的指針移道下一行
f.read() 一次讀取全部內(nèi)容师幕。 read(5) 表示一次讀取5個字節(jié)的內(nèi)容
</pre>
文件的寫入
<pre>
文件的寫入可以使用write()和writelines()方法寫入文件
context = 'hello world hello China hello Keke'
f = open('hello.txt','a') #以寫入的方式打開文件。追加新的內(nèi)容
再重新寫入新的內(nèi)容诬滩。如果文件不存在霹粥,則創(chuàng)建1個新的文件
f.write(context)
f.close()
其他函數(shù)
writelines()把列表存儲內(nèi)容寫到文件中。寫入大量文件是這用方式效率更好
</pre>
文件的刪除
文件的刪除需要用os模塊和os.path模塊疼鸟。os模塊提供了系統(tǒng)環(huán)境后控、文件、目錄等操作
系統(tǒng)級別的接口函數(shù)
<pre>
import os
f = open('hello.txt')
content = f.read() #一次讀完全部內(nèi)容
print(content)
f.close()
open()是os模塊中打開文件的函數(shù)空镜。與前面的file函數(shù)的用法一樣
if os.path.exists('hello.txt'): #判斷文件是否存在
print('存在')
os.remove('hello.txt') #如果存在 刪除文件
其他函數(shù)
os.rename(old,new) #對文件沖命名
os.start(path) #返回文件的屬性
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=50L, st_atime=1464879907L, st_mtime=1465132977L, st_ctime=1464879907L)
os.startfile(filepath[,operation]) #啟動關(guān)聯(lián)程序打開文件
os.startfile('hello.txt','notepad') #用記事本打開hello.txt文件
-------------------------------------------#
os.path模塊的函數(shù)
abspath(path) #返回path所在的絕對路徑
exists(path) #判斷文件是否存在
getatime(filename) #返回文件的最后訪問時間
getctime(filename) #返回文件的創(chuàng)建時間
getmtime(filename) #返回文件最后修改時間
getsize(filename) #返回文件大小
split(p) #對路徑進行分割
splitext(p) #分割出文件的而擴展名 ('hello', '.txt')
</pre>
文件的復(fù)制
file類中沒有提供直接復(fù)制文件的方法浩淘,但可以使用read()、write()方法模擬copy功能
<pre>
src = file('hello.txt','w')
li = ['hello world\n','hello China\n']
scr.writelines(li) #寫入列表打文件中
scr.close()
文件的寫入
src = file('hello.txt','r')
dst = file('hello2.txt','w')
dst.write(src.read())
src.close()
dst.close()
</pre>
shutil模塊是另一個文件吴攒、目錄的管理接口张抄。用于復(fù)制文件和目錄
<pre>
import shutil
shutil.copyfile('hello.txt', 'hello1.txt')
shutil.move('hello.txt','../') #移動文件
shutil.move('hello.txt','hello2.txt') #移動文件到當(dāng)前目錄并命名為hello2,然后刪除源文件
</pre>
文件查找匹配用glob模塊
<pre>
import glob
li = glob.glob('*.txt') 用與匹配當(dāng)前目錄下.txt后綴的文件 并返回一個列表
print(li)
</pre>