用 w+方式打開后,再使用read語(yǔ)句后出現(xiàn)問題
python 文件操作。以下是命令行的輸入與輸出結(jié)果。(附在問題后面)
當(dāng)f.write("it's easy.")寫入后,2.py文件內(nèi)容應(yīng)該變成"it's easy."
進(jìn)行到這步時(shí),我打開2.py文件,文件內(nèi)容的確如此,
但當(dāng)我把這個(gè)文件的值讀出來(lái)賦給c,為什么打印出來(lái)的內(nèi)容并不是"it's easy."而是一大堆亂碼了?
我再打開2.py文件,里面的內(nèi)容也是一堆亂碼,問題出在哪里呢?
============
>>> f=open('2.py','w+')
>>> c=f.read()
>>> c
''
>>> f.write("it's easy.")
>>> c=f.read()
>>> c
'\xdf\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\xbfn\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x98\xc0n\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00(\x83f\x00\x00\x00\x00\x00\x01\x00\x00\x00l\x03\x00Z\xf8@i\x00\x00\x00\x00\x00\x02\x00\x00\x00\x04\x00d\x01\x00d\x02\x00l\x05\x00Z\x01\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\xf0\xbfn\x00\x00\x00\x00\x00\xe8Qa\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00(\xc0n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00`\xc0n\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00p\xc0n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xe0\xbfn\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\xf0\xbfn\x00\x00\x00\x00\x00\xa8\xc0n\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x05\x00\x00\x00\x0f\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xe0\xbfn\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\xe0\xbfn\x00\x00\x00\x00\x00\x05\x00\x00\x00d)\x00\x84\xf0\xbfn\x00\x00\x00\x00\x00\x08\xc1n...
2020-02-13
第一記得關(guān)閉文件.第二試試wb+的操作.有幫助的話可以給個(gè)最佳回答嗎?