刪除檔案中的某一行以及檔案鎖

2021-10-01 06:18:29 字數 4518 閱讀 7610

gpu.dat的內容如下

81e443bd-b826-4459-999d-685fa63e8a88:/dev/dri/renderd128

166a4eab-fc0d-4264-a35d-8b78e09f22ea:/dev/dri/renderd128

56e846d8-b422-4720-b3e5-c2813b756b9b:/dev/dri/renderd128

2b1a1ecb-357f-4adc-9639-97bf6c8a8e9f:/dev/dri/renderd128

0492e933-0e1f-44c0-b2f3-80f4966bb8ae:/dev/dri/renderd128

b8f4710f-efc1-471b-9055-34fe7261d98b:/dev/dri/renderd128

6214c986-3ba2-420a-a3db-5a1411841de1:/dev/dri/renderd128

c960ee33-f8c2-4255-bbe9-1e971cf4065b:/dev/dri/renderd128

8bb6c888-1f03-4898-b396-4f57ca1571c9:/dev/dri/renderd128

d7afdf91-5ae8-41b9-9610-4a1a73fa3560:/dev/dri/renderd128

55a5a399-aed5-49d3-8fb4-bd16816ba3b2:/dev/dri/renderd128

576bce10-d36a-4a4c-9202-e2a55aa29b89:/dev/dri/renderd128

想要刪除指定uuid的某一行,以及多程序呼叫這個讀寫檔案時怎麼實現。

import os

dir=

'test'

gpu_file = os.path.join(

dir,

'gpu.dat'

)if os.path.exists(gpu_file)

: lines =

[l for l in

open

(gpu_file,

"r")

if l.find(

'c960ee33-f8c2-4255-bbe9-1e971cf4065b',0

,36)!=

0]fd =

open

(gpu_file,

"w")

fd.writelines(lines)

fd.close(

)

gpu.dat相當於乙個資料庫,新增刪除都要做記錄,但是當多程序呼叫時,如乙個正開啟檔案做新增資料操作,另乙個程序卻要開啟檔案做刪除資料記錄,必然會造成檔案記錄的資料混亂,雖然多程序可以用加鎖來解決競爭呼叫,但是這是從執行的程式來說,而檔案鎖則要簡單的多。

標誌位含義

lock_sh

表示要建立乙個共享鎖,所有程序沒有寫訪問許可權,即使是加鎖程序也沒有。所有程序有讀訪問許可權,在任意時間內,乙個檔案的共享鎖可以被多個程序擁有。

lock_ex

表示建立乙個排他鎖,在任意時間內,乙個檔案的排他鎖只能被乙個程序擁有

lock_un

表示刪除該程序建立的鎖

lock_nb

如果指定此引數,函式不能獲得檔案鎖就立即返回,否則,函式會等待獲得檔案鎖。lock_nb可以同lock_sh或lock_nb進行按位或(|)運算操作。 fcnt.flock(f,fcntl.lock_ex|fcntl.lock_nb)

通常情況下,如果加鎖請求不能被立即滿足,那麼系統呼叫 flock()會阻塞當前程序。比如,程序想要請求乙個排他鎖,但此時,已經由其他程序獲取了這個鎖,那麼該程序將會被阻塞。

注意:

對於檔案的 close() 操作會使檔案鎖失效;

同理,程序結束後檔案鎖失效;

flock() 的 lock_ex是「勸告鎖」,系統核心不會強制檢查鎖的狀態,需要在**中進行檔案操作的地方顯式檢查才能生效。

import fcntl

import os

dir=

'test'

gpu_file = os.path.join(

dir,

'gpu.dat'

)if os.path.exists(gpu_file)

: lines =

[l for l in

open

(gpu_file,

"r")

if l.find(

'c960ee33-f8c2-4255-bbe9-1e971cf4065b',0

,36)!=

0]fd =

open

(gpu_file,

"w")

fcntl.flock(fd, fcntl.lock_ex)

fd.writelines(lines)

fcntl.flock(fd, fcntl.lock_ex)

fd.close(

)

示例

import fcntl

import os

import sys

import threading

import time

dir=

'test'

defwriting

(name)

:"""寫操作"""

gpu_file = os.path.join(

dir,

'test.dat')if

not os.path.exists(gpu_file)

: os.mknod(gpu_file)

with

open

(gpu_file,

'a')

as f:

fcntl.flock(f, fcntl.lock_ex)

#對檔案進行加鎖

f.write(name +

'\n'

) fcntl.flock(f, fcntl.lock_un)

#對檔案進行解鎖

defdeleting

(name)

:"""刪操作"""

gpu_file = os.path.join(

dir,

'test.dat'

)if os.path.exists(gpu_file)

: lines =

[l for l in

open

(gpu_file,

"r")

if l.find(name,0,

1)!=0

]print

('lines=%s'

%lines)

fd =

open

(gpu_file,

"w")

fcntl.flock(fd, fcntl.lock_ex)

fd.writelines(lines)

fcntl.flock(fd, fcntl.lock_un)

fd.close(

)else

:print

('檔案不存在呢,拜拜了您!'

) sys.exit(1)

if __name__ ==

'__main__'

: p_d = multiprocessing.pool(20)

p_w = multiprocessing.pool(20)

a =[chr

(i)for i in

range(65

,91)]

#26個大寫字母

for i in a:

threading.thread(target=writing, kwargs=

).start(

) a.remove(a[-1

])for j in a:

i ='@@'

+ j(i,))

p_w.close(

) p_w.join(

)

執行後的結果

z

@@a@@u

@@v@@w

@@x@@y

@@b@@c

@@d@@e

@@f@@g

@@h@@i

@@j@@k

@@l@@m

@@n@@o

@@p@@q

@@r@@s

@@t

如果不加檔案鎖,檔案記錄結果可能會發生混亂。

檢視官方文件說明可以知道,python中的fcntl是對unix的fcntl進行封裝,並且可以細節到對檔案的一部分進行加鎖,這部分可以參考python的fcntl官方文件說明。

os.path.exists(gpu_file)

os.path.join(

dir,

'test.dat'

)

更改檔案,某一行

1 deffetch data 2print 這是查詢功能 3 tag false 4 data aaaa s n data 5 res data 6 with open bbb r encoding gbk as file 7for i in file 8if i data 9 tag true ...

python刪除某一行

整理了網路上的一些方法,一般有兩種方法 第一種 是先把檔案讀入記憶體,在記憶體中修改後再寫入原始檔。例子 將內容包含 123 的所有行刪去 with open c users lai desktop 1.txt r as r lines r.readlines with open c users l...

Linux 刪除和替換檔案中某一行的方法

如果有乙個abc.txt檔案,內容是 aaabbb batbusinesstype,insideid online insideid online cccddd eeefff 如果要刪除ddd,那麼指令碼可以這樣寫 sed i ddd d abc.txt 如果刪除的是乙個變數的值,假如變數是var,...