1疗绣、打開文件
在python线召,使用open函數(shù),可以打開一個(gè)已經(jīng)存在的文件多矮,或者創(chuàng)建一個(gè)新文件
open(文件名缓淹,訪問(wèn)模式)
示例如下:
f = open('test.txt', 'w')
說(shuō)明:
2、關(guān)閉文件
close( )
示例如下:
#新建一個(gè)文件塔逃,文件名為:test.txt
f = open('test.txt', 'w')
#關(guān)閉這個(gè)文件
f.close()
3讯壶、路徑
linux
windows
4、文件的讀寫
寫數(shù)據(jù)(write)
使用write()可以完成向文件寫入數(shù)據(jù)
demo:
f = open('test.txt', 'w')
f.write('hello world, i am here!')
f.close()
注意:
?如果文件不存在那么創(chuàng)建湾盗,如果存在那么就先清空伏蚊,然后寫入數(shù)據(jù)
讀數(shù)據(jù)(read)
使用read(num)可以從文件中讀取數(shù)據(jù),num表示要從文件中讀取的數(shù)據(jù)的長(zhǎng)度(單位是字節(jié))格粪,如果沒(méi)有傳入num躏吊,那么就表示讀取文件中所有的數(shù)據(jù)
demo:
f = open('test.txt', 'r')
content = f.read(5)
print(content)
print("-"*30)
content = f.read()
print(content)
f.close()
注意:
如果open是打開一個(gè)文件氛改,那么可以不用寫打開的模式,即只寫open('test.txt')
如果使用讀了多次比伏,那么后面讀取的數(shù)據(jù)是從上次讀完后的位置開始的
讀數(shù)據(jù)(readlines)
就像read沒(méi)有參數(shù)時(shí)一樣胜卤,readlines可以按照行的方式把整個(gè)文件中的內(nèi)容進(jìn)行一次性讀取,并且返回的是一個(gè)列表赁项,其中每一行的數(shù)據(jù)為一個(gè)元素
f = open('test.txt', 'r')
content = f.readlines()
print(type(content))
i=1
for temp in content:
print("%d:%s"%(i, temp))
i+=1
f.close()
讀數(shù)據(jù)(readline)
f = open('test.txt', 'r')
content = f.readline()
print("1:%s"%content)
content = f.readline()
print("2:%s"%content)
f.close()
如果一個(gè)文件很大葛躏,比如5G,試想應(yīng)該怎樣把文件的數(shù)據(jù)讀取到內(nèi)存然后進(jìn)行處理呢悠菜?
#把舊文件中的數(shù)據(jù)一行一行復(fù)制到新文件中
while 1:
count=oldfile.read(1024) ????#(按1024個(gè)字節(jié)復(fù)制)
if count=='':
break
newfile.write(count)
oldfile.close()
newfile.close()
方法二:
for linecontent in oldfile.readlines():
newfile.write(linecontent)
oldfile.close()
newfile.close()
5舰攒、應(yīng)用1:制作文件的備份
任務(wù)描述:
輸入文件的名字,然后程序自動(dòng)完成對(duì)文件進(jìn)行備份
參考代碼:
oldFileName = input("請(qǐng)輸入要拷貝的文件名字:")
oldFile = open(oldFileName,'r')
#如果打開文件
if oldFile:
#提取文件的后綴
fileFlagNum = oldFileName.rfind('.')
if fileFlagNum > 0:
fileFlag = oldFileName[fileFlagNum:]
#組織新的文件名字
newFileName = oldFileName[:fileFlagNum] + '[復(fù)件]' + fileFlag
#創(chuàng)建新文件
newFile = open(newFileName, 'w')
#把舊文件中的數(shù)據(jù)李剖,一行一行的進(jìn)行復(fù)制到新文件中
for lineContent in oldFile.readlines():
newFile.write(lineContent)
#關(guān)閉文件
oldFile.close()
newFile.close()
6芒率、文件的隨機(jī)讀寫
6.1、獲取當(dāng)前讀寫的位置
在讀寫文件的過(guò)程中篙顺,如果想知道當(dāng)前的位置偶芍,可以使用tell()來(lái)獲取
從0開始到字符的個(gè)數(shù)
#打開一個(gè)已經(jīng)存在的文件
f = open("test.txt", "r")
str = f.read(3)
print ("讀取的數(shù)據(jù)是: ", str)
#查找當(dāng)前位置
position = f.tell()
print ("當(dāng)前文件位置: ", position)
str = f.read(3)
print ("讀取的數(shù)據(jù)是: ", str)
#查找當(dāng)前位置
position = f.tell()
print ("當(dāng)前文件位置: ", position)
f.close()
6.2、定位到某個(gè)位置
如果在讀寫文件的過(guò)程中德玫,需要從另外一個(gè)位置進(jìn)行操作的話匪蟀,可以使用seek()
seek(offset, from)有2個(gè)參數(shù)
1.offset:偏移量
2.from:方向
a)0:表示文件開頭(python3)
b)1:表示當(dāng)前位置(python2)
c)2:表示文件末尾(python2)
demo:把位置設(shè)置為:從文件開頭,偏移5個(gè)字節(jié)
#打開一個(gè)已經(jīng)存在的文件
f = open("test.txt", "r")
str = f.read(30)
print ("讀取的數(shù)據(jù)是: ", str)
#查找當(dāng)前位置
position = f.tell()
print ("當(dāng)前文件位置: ", position)
#重新設(shè)置位置
f.seek(5,0)
#查找當(dāng)前位置
position = f.tell()
print ("當(dāng)前文件位置: ", position)
f.close()
demo:把位置設(shè)置為:離文件末尾宰僧,3字節(jié)處
#打開一個(gè)已經(jīng)存在的文件
f = open("test.txt", "r")
#查找當(dāng)前位置
position = f.tell()
print ("當(dāng)前文件位置: ", position)
#重新設(shè)置位置
f.seek(-3,2)
#讀取到的數(shù)據(jù)為:文件最后3個(gè)字節(jié)數(shù)據(jù)
str = f.read()
print ("讀取的數(shù)據(jù)是: ", str)
f.close()
7材彪、文件的重命名、刪除
7.1琴儿、文件重命名
os模塊中的rename()可以完成對(duì)文件的重命名操作
rename(需要修改的文件名,新的文件名)
import os
os.rename("畢業(yè)論文.txt", "畢業(yè)論文-最終版.txt")
7.2段化、刪除文件
os模塊中的remove()可以完成對(duì)文件的刪除操作
remove(待刪除的文件名)
import os
os.remove("畢業(yè)論文.txt")
8、文件夾的相關(guān)操作
8.1造成、創(chuàng)建文件夾
import os
os.mkdir("張三")
8.2显熏、獲取當(dāng)前目錄
import os
os.getcwd()
8.3、改變默認(rèn)目錄
import os
os.chdir("../")
8.4晒屎、獲取目錄列表
import os
os.listdir("./")
8.5喘蟆、刪除文件夾
import os
os.rmdir("張三")
9、應(yīng)用2:批量修改文件名
#批量在文件名前加前綴
import os
funFlag = 1 # 1表示添加標(biāo)志2表示刪除標(biāo)志
folderName = './renameDir/'
#獲取指定路徑的所有文件名字
dirList = os.listdir(folderName)
#遍歷輸出所有文件名字
for name in dirList:
print name
if funFlag == 1:
newName = '[雍哥出品]-' + name
elif funFlag == 2:
num = len('[雍哥出品]-')
newName = name[num:]
print newName
os.rename(folderName+name, folderName+newName)