lesson5_oa文件操作

# 打开、关闭、修改、新建、保存、读取
# 打开一个文件 python中为转义字符,路径要多加一个为\双斜杠
fs = open("C:\pycharm-workspace\python6_basic\test1.txt",encoding="utf-8")
# print(fs.read())
# print("=================")
# print(fs.read())

# 按行读取 多行
lines = fs.readlines()
print(lines)
# 单行 已被读取过,运行为空
fs.readline()

# 写 直接把源文件覆盖
fs1 = open("C:\pycharm-workspace\python6_basic\test1.txt","w+",encoding="utf-8")
fs1.write("学习文件操作!
")
fs1.write("哈哈哈哈哈哈哈哈enenen")
fs.close()

# a 追加 tell()查看文件位置
fs1 = open("C:\pycharm-workspace\python6_basic\test1.txt","a",encoding="utf-8")
fs1.write("学习文件操作!
")
fs1.write("哈哈哈哈哈")
a = fs1.tell()
print(a)

file.closed  返回ture(文件已被关闭);返回false(如果未关闭)

file.mode  返回被打开文件的访问模式

file.name  返回文件的名称

原文地址:https://www.cnblogs.com/zhangniannian/p/11794162.html