2022.11.16 本学习内容总结于莫烦python:4.文件管理
https://mofanpy.com/tutorials/python-basic/interactive-python/read-write-file
均是用特殊字符open
f = open("new_file.txt", "w") # 创建并打开
f.write("some text...") # 在文件里写东西
f.close() # 关闭
w是写权限,会有相应的读等等(后面讲)…
|- me.py
|- new_file.txt
with open("new_file2.txt", "w") as f:
f.writelines(["some text for file2...\n", "2nd line\n"])
w 改成 r。 也就是说,其实 w 代表的是 write, r 代表的是 read
f = open("new_file2.txt", "r")
print(f.read())
f.close()
some text for file2...
2nd line
writelines(),那么在读文件的时候, 也可以 readlines() 直接读出来一个列表with open("new_file2.txt", "r") as f:
print(f.readlines())
['some text for file2...\n', '2nd line\n']
输出是个列表【】
with open(