data = open("file").read()
# print(data)
# 只读
file = open("file", 'r') # 文件句柄
data = file.read()
print(data)
# 只写 创建一个新文件
file2 = open("file2", "w")
file2.write("我爱Python")
# 可追加 不可读
file3 = open("file2", "a")
file3.write("呵呵呵呵")
# 高效循环,需要自己计算下标
file = open("file")
for i in file:
print(i)
# 操作光标
# file = open("file")
# print("光标位置",file.tell())
# print(file.readline())
# print(file.tell())
# file.seek(0)#
# print(file.tell())
# 只写截断
file = open("file",'w')
file.truncate(1)
# file = open("file", 'r+') # 读写
# file = open("file", 'w+') # 写读
# file = open("file", 'a+') # 追加读写
# file = open("file", 'rb') # 读二进制文件
# file = open("file", 'wb') # 写二进制文件
# print(file.readline())
# print(file.readline())
# print(file.readline())
# file.write("哈哈哈哈\n")
# file.write("呵呵呵\n")
# 更改文件
# file_old = open("file")
# file_new = open("file2",'w')
#
# for i in file_old:
# if "这是寿光唯一的山。" in i:
# i = i.replace("这是寿光唯一的山。","这是寿光全部的山。")
# file_new.write(i)
# 操作完自动关闭
# with open("file", "r") as f, \
# open("file", "r") as f2:
# print(f.readline())
网友评论