【筆試題】python文件操作


請說出下面代碼結果及原因,很easy。

 說明:test.txt文件不存在

 

with open('test.txt','w+') as f:
    f.write('34')
    f.seek(0)
    f.write('12')
    f.seek(0)
    print(f.read()) 

輸出結果及原因:  

 

with open('test.txt','a+') as f:
    f.write('34')
    f.seek(0)
    f.write('12')
    f.seek(0)
    print(f.read())  

輸出結果及原因:

 

with open('test.txt','a+') as f:
    f.write('3456789')
    f.seek(2)
    f.truncate(3)
    f.write('12')
    f.seek(0)
    print(f.read()) 

輸出結果及原因: 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM