Python之 檔案操作

2021-08-27 05:25:54 字數 3400 閱讀 8004

1、開啟檔案

f=open('hello.txt','a+')   #開啟『hello.txt』檔案

#r 唯讀模式,開啟的檔案不存在的話,會報錯

#w 只寫模式,會清空原來檔案的內容

#a+ 追加模式 開啟的檔案不存在,會新建乙個檔案

2、讀寫檔案

f=open('hello.txt','a+')

f.write('joly,123\npop,456')

#f.seek(0)

print(f.read()) #獲取到檔案裡的所有內容

#執行這個發現沒有結果?原來在檔案寫完內容以後,檔案指標挪到了最後,後面沒內

#容,自然沒有東西 這個時候引入檔案只針對概念f.seek(0),就是把檔案指標挪到開頭

#然後就讀出東西了

f=open('hello.txt','a+')

print(f.readlines())#以列表的形式展示出來

f=open('hello.txt','a+')

print(f.readline()) #顯示指標所在那一行的資料

inf=['uiui,890\n','hhh,009\n']

f.writeable(inf) #把列表寫進檔案裡

f.write() #如果寫字串的話,就直接用write就好

#r,w,a+這三種模式以外,還有r+ w+

f=open('hello.txt','r+')

f.write('wwww,123') #r+模式,開啟不存在的檔案還是會報錯,寫的東西顯示在檔案首

f=open('hello.txt','w+')

f.write

#還是會清空檔案,讀出來的東西是空,因為檔案清空沒內容,很雞肋,不推薦

#開啟非文字檔案,引數要加b 如rb wb

3、高效處理檔案

如果檔案過大,最後一行一行讀,提高效率

#第一種方式:

f=open('hello.txt',encoding='utf-8')

while

true:

line=f.readline()

ifline!='':

print(line)

else:

print('檔案內容讀完了')

break

#第二種方式:

for i in f:

print(i)

寫乙個花式操縱檔案的題

1、要從日誌裡面找到1分鐘之內訪問超過200次的

2、每分鐘都執行一次

3、超過200次的加入黑名單

思路:

1、讀取檔案內容,獲取到ip位址

2、把每個ip位址存起來 {}

3、判斷ip訪問的次數是否超過200次

4、加入黑名單 print

import time

point = 0

#初始的位置

while true:

ips = {}

f = open('access.log',encoding='utf-8')

f.seek(point)

forline

in f: #迴圈取檔案裡面每行資料

ip = line.split()[0] #按照空格分割,取第乙個元素就ip

if ip in ips:#判斷這個ip是否存在

# ips[ip] = ips[ip]+1

ips[ip]+=1

#如果存在的話,次數加+1

else:

ips[ip]=1

#如果不存在ip的次數就是1

point = f.tell() #記錄檔案指標位置

f.close()

for ip,count in ips.items():#迴圈這個字典,判斷次數大於200的

if count>=200:

print('%s 加入黑名單'%ip)

time.sleep(60)

3、修改檔案

f = open('c:\users\nhy\desktop\file.txt',encoding='utf-8')    #錯的

f = open(r'c:\users\nhy\desktop\file.txt',encoding='utf-8') #對的

#檔案如果寫的是絕對路徑,如上圖,容易有\n這種錯誤,這個時候,要寫r'絕對路徑'

#第一種方式:

f = open('file.txt',encoding='utf-8')

t = f.read().replace('一點','兩點')

f.close() #先替換

f = open('file.txt',mode='w',encoding='utf-8') #用w模式,刪除原檔案f.write(t) #寫入新檔案

f.flush() #立即把緩衝區裡面的內容,寫到磁碟上

f.close()

f = open('file.txt','a+',encoding='utf-8')

f.seek(0)

res = f.read().replace('你','ni')

f.seek(0)

f.truncate() #清空檔案裡的內容

f.write(res)

f.close()

import os #匯入模組

f = open('file.txt',encoding='utf-8')

f2 = open('cpfile.txt','w',encoding='utf-8')

forline

in f:

new_line = line.replace('兩點','yi點')

f2.write(new_line)

f.close()

f2.close()

os.remove('file.txt') #刪除原檔案

os.rename('cpfile.txt','file.txt') #給新檔案改名

import os #如果嫌關閉檔案麻煩,可以用我with as這個關鍵字

with

open('file.txt',encoding='utf-8') as f, open('file.txt.bak','w',encoding='utf-8') as f2:

forline

in f:

new_line = line.replace('二點','一點')

f2.write(new_line)

os.remove('file.txt')

os.rename('file.txt.bak','file.txt')

Python之檔案操作

file open filename,mode mode預設為 r 例如file ope test.txt r 以讀的方式開啟檔案.檔案操作完畢記得關閉.file.close 其中,mode可以有以下選擇 檔案test.txt的內容為 11111111111 aaaaaaaaa 2222222222...

Python之檔案操作

使用open w 以寫入模式開啟,如果檔案存在將會刪除裡面的所有內容,然後開啟這個檔案進行寫入 a 以追加模式開啟,寫入到檔案中的任何資料將自動新增到末尾 fobj open home coder documents obama.txt 唯讀開啟 fobj fobj.close 關閉檔案 fobj ...

Python之檔案操作

建立目錄import os import errno defmkdir dir try os.makedirs dir except oserror as exc if exc.errno errno.eexist print the dir has been existed pass else r...