日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python file read和write的速度_python file.truncate() 然后 file.write() file.read() 出现乱码...

發布時間:2025/3/15 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python file read和write的速度_python file.truncate() 然后 file.write() file.read() 出现乱码... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

##dirs0.txt 內容:

1234567890123456789012345678901234567890123456789012345678901234567890

def test(flag=None):

with open('dirs0.txt', 'r+') as f:

print f.read(8)

f.truncate()

f.write('+')

f.write('*')

if flag:

f.read()

test()

##測試

test() 執行結果正常, dirs0.txt 內容變為: 12345678+*

test(True) 執行結果異常, dirs0.txt 內容有亂碼:

12345678+34567890123456789012345678901234567890123456789012345678901234567890elib.rpc

unpickle_code

p4

(S'c\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\[email?protected]\x00\x00\x00s\x1a\x00\x00\x00d\x02\x00d\x00\x00\x84\x01\x00Z\x01\x00e\x01\x00d\x01\x00\x83\x01\x00\x01d\x02\x00S(\x03\x00\x00\x00c\x01\x00\x00\x00\x02\x00\x00\x00\x06\x00\x00\x00C\x00\x00\x00sa\x00\x00\x00t\x00\x00d\x01\x00d\x02\x00\x83\x02\x00\x8fL\x00}\x01\x00|\x01\x00j\x01\x00d\x03\x00\x83\x01\x00GH|\x01\x00j\x02\x00\x83\x00\x00\x01|\x01\x00j\x03\x00d\x04\x00\x83\x01\x00\x01|\x01\x00j\x03\x00d\x05\x00\x83\x01\x00\x01|\x00\x00rW\x00|\x01\x00j\x01\x00\x83\x00\x00\x01n\x00\x00Wd\x00\x00QXd\x00\x00S(\x06\x00\x00\x00Ns\t\x00\x00\x00dirs0.txts\x02\x00\x00\x00r+i\x08\x00\x00\x00t\x01\x00\x00\x00+t\x01\x00\x00\x00(\x04\x00\x00\x00t\x04\x00\x00\x00opent\x04\x00\x00\x00readt\x08\x00\x00\x00truncatet\x05\x00\x00\x00write(\x02\x00\x00\x00t\x04\x00\x00\x00flagt\x01\x00\x00\x00f(\x00\x00\x00\x00(\x00\x00\x00\x00s!\x00\x00\x00C:\Users\texs\Desktop\testfile.pyt\x04\x00\x00\x00test\x02\x00\x00\x00s\x0e\x00\x00\x00\x00\x01\x12\x01\x0e\x01\n\x01\r\x01\r\x01\x06\x01i\x01\x00\x00\x00N(\x02\x00\x00\x00t\x04\x00\x00\x00NoneR\x08\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s!\x00\x00\x00C:\Users\texs\Desktop\testfile.pyt\x08\x00\x00\x00\x02\x00\x00\x00s\x02\x00\x00\x00\x0c\t'

tRp5

tp6

(dp7

ttp8

tp9

. S R& 垶_ 訸 ?  c @ s d d ? Z e d ? d S( c C sa t d d ? 廘 } | j d ? GH| j ? | j d ? | j d ? | rW | j ? n Wd QXd S( Ns dirs0.txts r+i t +t *( t opent readt truncatet write( t flagt f( ( s! C:\Users\texs\Desktop\testfile.pyt test s

i N( t NoneR ( ( ( s! C:\Users\texs\Desktop\testfile.pyt s ... ...

##說明:

**f.truncate 之后, f.write() 和 f.read() 同時使用才會有亂碼,單獨使用 write() 或 read() 沒有亂碼

發現 f.write('+') f.write('*') 占用了兩個字,然后追加到 dirs0.txt 文件尾部的內容是原始文件內容除去頭部兩個字的部分

+*34567890123456789012345678901234567890123456789012345678901234567890

1234567890123456789012345678901234567890123456789012345678901234567890

進一步有了刪除文件中指定行代碼(會在文件后添加亂碼):

def test(flag=None, lineno=1):

with open('dirs0.txt', 'r+') as f:

for n in range(lineno-1):

f.readline()

tempno1 = f.tell()

f.readline()

tempno2 = f.tell()

f.truncate(tempno1)

f.write(' '*(tempno2-lineno-1)+'\n')

if flag:

f.read()

test(1, lineno=3)

最后

問題:

-1 ) f.read() 為啥會有內容寫入文件,而且 f.write() 和 f.read() 要一起用才會有寫入?

-2) 為什么會出現亂碼?

多謝大俠指教^_^

總結

以上是生活随笔為你收集整理的python file read和write的速度_python file.truncate() 然后 file.write() file.read() 出现乱码...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。