python学习-文件创建读取

# 文件创建 # 读写
# 文件存在?不存在?在操作系统上
# 读 read r 写 write w
# 打开一个文件
# fs = open("xiaojian.txt",encoding="utf-8")
# print(fs)

# 读取
# person_info = fs.read() # n 指定读出多少字节 默认是全部读取出来的。
# print(person_info)
# print("======================")
# per2 = fs.read()
# print(per2)

# # 按行读取
# line1 = fs.readline()
# print(line1)
# print("************************")
# line2 = fs.readline()
# print(line2)
# print("**********************")
# line3 = fs.readline(3)
# print(line3)

# 写 w
# write不会主动换行。需要你加上\n来换行。
# 如果文件存在,则会覆盖原来的内容。如果文件不存在,则会创建文件。
# fs = open("movies.txt","w",encoding="utf-8")
# fs.write("我喜欢的电影有:复仇者联盟4。\n") # 写
# fs.write("我喜欢的动漫有:斗破苍穹。\n我喜欢的班级有:class17\n")
# # fs.write("我喜欢的班级有:class17")
# # 写入多行 参数是列表 它也不是会主动换行。需要你加上\n来换行。
# # fs.writelines(["我是ruby\n","我是在路上\n","我喜欢吃肉\n"])
# fs.close() # 文件关闭

# 追加 a
# 如果文件不存在,则创建再追加写。如果存在,则在文件末尾追加内容
fs = open("D:\\Pychram-Workspace\\python17\\class_20190420\\movies.txt","a",encoding="utf-8")
fs.write("\n我准备一放假就去看复仇者联盟,同学们不要剧透!!")
fs.read()
fs.close() # 关闭。

# 先写完,关闭文件。再打开文件,去读取。
# 文件:外部资源。有借有还。

# 异常。

猜你喜欢

转载自www.cnblogs.com/qsmyjz/p/11261227.html