話不多說直接碼
# 絕對路徑
# f = open('/Users/fangxiang/Downloads/我的古詩.text', mode='r', encoding='utf-8')
# content = f.read()
# print(content)
# f.close()
# 相對路徑
f = open('我的古詩.text', mode='r', encoding='utf-8')
content = f.read()
print(content, type(content))
print(f, type(f))
f.close()
# 1、打開非文字類文件的讀取看
# 2、上傳下載儲存時
f = open('我的古詩.text', mode='rb')
content = f.read()
print(content)
f.close()
# 寫 w
# 對於寫 沒有此文件就創建文件
# f = open('log', mode='w', encoding='utf-8')
# f.write('高清五碼')
# f.close()
# 先將源文件的內容全部清除,再寫
# f = open('log', mode='w', encoding='utf-8')
# f.write('絕對好看')
# f.close()
# # 寫wb 以bytes方式寫入
# f = open('log', mode='wb')
# f.write('附件看到類型節分'.encode('utf-8'))
# f.close()
# 追加 a
# f = open('log', mode='a', encoding='utf-8')
# f.write('追加進去的內容1')
# f.close()
# # 追加 ab 以bytes方式追加
# f = open('log', mode='ab')
# f.write('追加進去的內容2'.encode('utf-8'))
# f.close()
# 讀寫 r+ 先把原文章讀出來,再追加進去 也有bytes類型 r+b
# f = open('log', mode='r+', encoding='utf-8')
# content = f.read()
# print(content)
# f.write(',讀后追加的內容')
# f.close()
# r+先寫后讀(光標從頭開始寫,再讀取光標后的內容)
# f = open('log', mode='r+', encoding='utf-8')
# f.write('wer')
# content = f.read()
# print(content)
# f.close()
# r+b 讀寫 bytes形式
# f = open('log', mode='r+b')
# content = f.read()
# print(content)
# f.write(',讀后追加的內容'.encode('utf-8'))
# f.close()
# 寫讀 w+ 寫后在讀就讀不出來了 w+b
# f = open('log', mode='w+', encoding='utf-8')
# f.write('先寫,厚度'),
# print(f.read())
# f.close()
# 追加 a+
# f = open('log', mode='a+', encoding='utf-8')
# f.write('——這里是追加的內容+')
# f.seek(0)
# print(f.read())
# f.close()
# a+b
# 功能詳解
# seek()
# f = open('log', mode='r+', encoding='utf-8')
# content = f.read(3) # 讀出來的都是字符 讀取三個字符
# print(content)
# f.seek(3) # 光標是按照字節去找的 是按照字節定光標
# 斷點續傳
# f.tell() 告訴你光標的位置(字節計算),然后在seek調到該位置
# print(f.tell())
# content = f.read()
# print(content)
# f.close()
# 全查看光標,在編號光標位置,最后讀取
# f = open('log', mode='a+', encoding='utf-8')
# f.write('——加起')
# count = f.tell()
# f.seek(count-9) # 讀取后三中文字符
# content = f.read()
# print(content)
# f.close()
# f.tell() 光標位置
# f.readable() 判斷是否可讀
# f.truncate(2) 對原文件進行截取 修改源文件 截取前2個字節
f = open('log', mode='r+', encoding='utf-8')
# f.truncate(3)
# line = f.readline() # 一行一行的讀
# lines = f.readlines() # 每一行當成列表中的一個元素,添加到list中
# print(line)
# print(lines)
# for line in f:
# print(line)
# f.close()
# 打開文件方式 不需要關閉文件f.close()
# 方式一
# with open('log', mode='r+', encoding='utf-8') as obj:
# print(obj.read())
# 方式二
with open('log', mode='r+', encoding='utf-8') as f,\
open('模特主婦護士老師.text', mode='r+', encoding='utf-8') as f1:
print(f1.read())
print(f.read())
