f = open("test.txt", "w")
f.write("hello world")
f.close()
f = open(r"F:\study_project\文件操作\test.txt", "w")
f.write("Student")
f.close()
f = open("test.txt", "w")
f.write("你好,世界")
f.close()
f = open("test.txt", "w", encoding="UTF-8")
f.write("你好,世界")
f.close()
f = open("test.txt", "w", encoding="UTF-8")
content = ["你好,世界", "人生苦短", "我用Python"]
f.write("\n".join(content))
f.close()
f = open("test2.txt", "w", encoding="UTF-8")
f.write("Python\n" * 10)
f.close()
f = open("test2.txt", "r", encoding="utf-8")
while True:
content = f.readline()
if content:
print(f"{content}", end="")
else:
break
f.close()
f = open("test2.txt", "r", encoding="utf-8")
content = f.readlines()
print(content)
i = 1
for data in content:
print(f"{i}:{data}", end="")
i +=1
f.close()
f = open("test3.txt", "w", encoding="utf-8")
f.write("你好,世界")
f.close()
f = open("test3.txt", "r", encoding="utf-8")
content = f.read(2)
print(content)
print("当前指针所在位置:", f.tell())
f.close()
f = open("test3.txt", "r", encoding="utf-8")
content = f.read(2)
print(content)
f.seek(4)
print("当前指针所在位置:", f.tell())

f = open("test4.txt", "r+", encoding="utf-8")
data = f.read()
f.write("我用Python")
f.seek(0)
f.write(" 我用Python")
f.close()
fin = open("文件操作.png", mode="rb")
fout = open("文件操作_copy.png", mode="wb")
while True:
data = fin.read(100)
if data != b"":
fout.write(data)
else:
break
fin.close()
fout.close()
f = open("test5.txt", "r+", encoding="utf-8")
f.seek(11)
f.truncate()
f.close()
f = open("test2.txt", "r", encoding="utf-8")
for line in f:
print(line, end="")
f.close()
f = open("test5.txt", "r")
print("文件名:", f.name)
print("文件打开的模式", f.mode)
print("文件可写:", f.writable())
print("文件可读:", f.readable())