Python 文件 IO

Document 對(duì)象參考手冊(cè) Python3 實(shí)例

以下代碼演示了Python基本的文件操作,包括 open,read,write:

# Filename : test.py
# author by : www.o2fo.com

# 寫(xiě)文件
with open("test.txt", "wt") as out_file:
    out_file.write("該文本會(huì)寫(xiě)入到文件中\(zhòng)n看到我了吧!")
 
# Read a file
with open("test.txt", "rt") as in_file:
    text = in_file.read()
 
print(text)

執(zhí)行以上代碼輸出結(jié)果為:

該文本會(huì)寫(xiě)入到文件中
看到我了吧!

Document 對(duì)象參考手冊(cè) Python3 實(shí)例