f = open("护?少妇嫩模.txt",mode="r", encoding="utf-8") content = f.read() print(content) f.close()
f = open("护?少妇嫩模.txt",mode="rb" ) content = f.read() print(content) f.close()
结果: b‘\xe6\xaf\x85\xe5\x93\xa5, \xe5\xa4\xaa\xe7\x99\xbd, wuse\n\xe5\x91\xb5\xe5\x91\xb5\n\xe6\x97\xa5\xe5\xa4\xa9‘
1. 绝对路径:从磁盘根?录开始?直到?件名。2. 相对路径:同?个?件夹下的?件,相对于当前这个程序所在的?件夹??,如果在同?个?件夹中,则相对路径就是这个?件名,如果在上?层?件夹,则要../。
f = open("../def/哇擦.txt", mode="r", encoding="utf-8") content = f.read() print(content)
结果: 友谊地久天?, 爱?点, 可惜我是?瓶座 ??中最爱
f = open("../def/哇擦.txt", mode="r" encoding="utf-8") content = f.read(3) print(content)
结果: 友谊地
f = open("../def/哇擦.txt", mode="rb") content = f.read(3) print(content)
结果: b‘\xe5\x8f\x8b‘
f = open("../def/哇擦.txt", mode="r", encoding="utf-8") content = f.read(3) content2 = f.read(3) print(content) print(content2)
结果: 友谊地 久天?
f = open("../def/哇擦.txt", mode="r", encoding="utf-8") content = f.readline() content2 = f.readline() content3 = f.readline() content4 = f.readline() content5 = f.readline() content6 = f.readline() print(content) print(content2) print(content3) print(content4) print(content5) print(content6)
结果: 友谊地久天?, 爱?点, 可惜我是?瓶座 ??中最爱
f = open("../def/哇擦.txt", mode="r", encoding="utf-8") lst = f.readlines() print(lst) for line in lst: print(line.strip())
f = open("../def/哇擦.txt", mode="r", encoding="utf-8") for line in f: print(line.strip())
f = open("?娃娃", mode="w", encoding="utf-8") f.write("??狮王") f.flush() # 刷新. 养成好习惯 f.close()
f = open("?娃娃", mode="w", encoding="utf-8") f.write("??狮王") f.read() # not readable 模式是w. 不可以执?读操作 f.flush() f.close()
f = open("?娃娃", mode="wb") f.write("??狮王".encode("utf-8")) f.flush() f.close()
f = open("?娃娃", mode="a", encoding="utf-8") f.write("麻花藤的最爱") f.flush() f.close()
f = open("?娃娃", mode="r+", encoding="utf-8") content = f.read() f.write("麻花藤的最爱") print(content) f.flush() f.close()
结果: 正常的读取之后, 写在结尾
f = open("?娃娃", mode="r+", encoding="utf-8") f.write("哈哈") content = f.read() print(content) f.flush() f.close() 结果: 将开头的内容改写成了"哈哈", 然后读取的内容是后?的内容.
f = open("?娃娃", mode="w+", encoding="utf-8") f.write("哈哈") content = f.read() print(content) f.flush() f.close()
f = open("?娃娃", mode="a+", encoding="utf-8") f.write("?化腾") content = f.read() print(content)
f.flush() f.close()
f = open("?娃娃", mode="r+", encoding="utf-8") f.seek(0) # 光标移动到开头 content = f.read() # 读取内容, 此时光标移动到结尾 print(content) f.seek(0) # 再次将光标移动到开头 f.seek(0, 2) # 将光标移动到结尾 content2 = f.read() # 读取内容. 什么都没有 print(content2)
f.seek(0) # 移动到开头 f.write("张国荣") # 写?信息. 此时光标在9 中?3 * 3个 = 9
f.flush() f.close()
f = open("?娃娃", mode="r+", encoding="utf-8") f.seek(0) # 光标移动到开头 content = f.read() # 读取内容, 此时光标移动到结尾 print(content) f.seek(0) # 再次将光标移动到开头 f.seek(0, 2) # 将光标移动到结尾 content2 = f.read() # 读取内容. 什么都没有 print(content2)
f.seek(0) # 移动到开头 f.write("张国荣") # 写?信息. 此时光标在9 中?3 * 3个 = 9
print(f.tell()) # 光标位置9
f.flush() f.close()
f = open("?娃娃", mode="w", encoding="utf-8") f.write("哈哈") # 写?两个字符 f.seek(3) # 光标移动到3, 也就是两个字中间 f.truncate() # 删掉光标后?的所有内容 f.close()
f = open("?娃娃", mode="r+", encoding="utf-8") content = f.read(3) # 读取12个字符 f.seek(4) print(f.tell()) f.truncate() # 后?的所有内容全部都删掉 # print(content) f.flush() f.close()
# ?件修改 import os with open("?娃娃", mode="r", encoding="utf-8") as f1, open("?娃娃_new", mode="w", encoding="UTF-8") as f2: content = f1.read() new_content = content.replace("冰糖葫芦", "??梨") f2.write(new_content) os.remove("?娃娃") # 删除源?件 os.rename("?娃娃_new", "?娃娃") # 重命名新?件
import os with open("?娃娃", mode="r", encoding="utf-8") as f1, open("?娃娃_new", mode="w", encoding="UTF-8") as f2: for line in f1: new_line = line.replace("??梨", "冰糖葫芦") f2.write(new_line) os.remove("?娃娃") # 删除源?件 os.rename("?娃娃_new", "?娃娃") # 重命名新?件
原文:https://www.cnblogs.com/liuhui0308/p/11809292.html