Python3 file文件基礎操作,文章內容包含了文件的創建,讀取等基礎操作。閒話少說,直接看代碼:
#讀文件
import os
filePath = os.path.join(os.getcwd() ,'subdict','a.txt' )
file = open(filePath,'r+') #讀+寫
print(file)
txt = file.read()
file.write('python')
print(txt)
#變量直接被釋放
with open(os.path.join(os.getcwd(),'subdict','a.txt' ),'a+') as fp:
txt = fp.read()
print(txt)
pass
#讀取二進制文件
content = ' '
with open((os.getcwd() + os.sep + '1565145725.jpg'),'rb') as fp:
content = fp.read()
pass
print(content)
#複製
with open((os.getcwd() + os.sep + '1565145725(1).png'),'wb') as fp:
fp.write(content)
pass
運行結束,會在當前目錄生成相應的文件。