发布于2020-01-01 11:34 阅读(996) 评论(0) 点赞(21) 收藏(1)
作者:@小灰灰
本文为作者原创,转载请注明出处:https://www.cnblogs.com/liuhui0308/p/11809292.html
阅读目录(Content)
- f = open("护⼠少妇嫩模.txt",mode="r", encoding="utf-8")
- content = f.read()
- print(content)
- f.close()
- f = open("护⼠少妇嫩模.txt",mode="rb" )
- content = f.read()
- print(content)
- f.close()
结果:- b'\xe6\xaf\x85\xe5\x93\xa5, \xe5\xa4\xaa\xe7\x99\xbd,
- wuse\n\xe5\x91\xb5\xe5\x91\xb5\n\xe6\x97\xa5\xe5\xa4\xa9'
1. 绝对路径:从磁盘根⽬录开始⼀直到⽂件名。2. 相对路径:同⼀个⽂件夹下的⽂件,相对于当前这个程序所在的⽂件夹⽽⾔,如果在同⼀个⽂件夹中,则相对路径就是这个⽂件名,如果在上⼀层⽂件夹,则要../。
- f = open("../def/哇擦.txt", mode="r", encoding="utf-8")
- content = f.read()
- print(content)
结果:- 友谊地久天⻓,
- 爱⼀点,
- 可惜我是⽔瓶座
- ⼀⽣中最爱
- f = open("../def/哇擦.txt", mode="r" encoding="utf-8")
- content = f.read(3)
- print(content)
结果:- 友谊地
f = open("../def/哇擦.txt", mode="rb")- content = f.read(3)
- print(content)
结果:- b'\xe5\x8f\x8b'
f = open("../def/哇擦.txt", mode="r", encoding="utf-8")- content = f.read(3)
- content2 = f.read(3)
- print(content)
- print(content2)
结果:- 友谊地
- 久天⻓
- f = open("../def/哇擦.txt", mode="r", encoding="utf-8")
- content = f.readline()
- content2 = f.readline()
- content3 = f.readline()
- content4 = f.readline()
- content5 = f.readline()
- content6 = f.readline()
- print(content)
- print(content2)
- print(content3)
- print(content4)
- print(content5)
- print(content6)
结果:- 友谊地久天⻓,
- 爱⼀点,
- 可惜我是⽔瓶座
- ⼀⽣中最爱
- f = open("../def/哇擦.txt", mode="r", encoding="utf-8")
- lst = f.readlines()
- print(lst)
- for line in lst:
- print(line.strip())
- f = open("../def/哇擦.txt", mode="r", encoding="utf-8")
- for line in f:
- print(line.strip())
- f = open("⼩娃娃", mode="w", encoding="utf-8")
- f.write("⾦⽑狮王")
- f.flush() # 刷新. 养成好习惯
- f.close()
- f = open("⼩娃娃", mode="w", encoding="utf-8")
- f.write("⾦⽑狮王")
- f.read() # not readable 模式是w. 不可以执⾏读操作
- f.flush()
- f.close()
- f = open("⼩娃娃", mode="wb")
- f.write("⾦⽑狮王".encode("utf-8"))
- f.flush()
- f.close()
- f = open("⼩娃娃", mode="a", encoding="utf-8")
- f.write("麻花藤的最爱")
- f.flush()
- f.close()
- f = open("⼩娃娃", mode="r+", encoding="utf-8")
- content = f.read()
- f.write("麻花藤的最爱")
- print(content)
- f.flush()
- f.close()
结果:- 正常的读取之后, 写在结尾
- f = open("⼩娃娃", mode="w+", encoding="utf-8")
- f.write("哈哈")
- content = f.read()
- print(content)
- f.flush()
- f.close()
- f = open("⼩娃娃", mode="a+", encoding="utf-8")
- f.write("⻢化腾")
- content = f.read()
- print(content)
f.flush()- f.close()
- f = open("⼩娃娃", mode="r+", encoding="utf-8")
- f.seek(0) # 光标移动到开头
- content = f.read() # 读取内容, 此时光标移动到结尾
- print(content)
- f.seek(0) # 再次将光标移动到开头
- f.seek(0, 2) # 将光标移动到结尾
- content2 = f.read() # 读取内容. 什么都没有
- print(content2)
f.seek(0) # 移动到开头- f.write("张国荣") # 写⼊信息. 此时光标在9 中⽂3 * 3个 = 9
f.flush()- f.close()
- f = open("⼩娃娃", mode="r+", encoding="utf-8")
- f.seek(0) # 光标移动到开头
- content = f.read() # 读取内容, 此时光标移动到结尾
- print(content)
- f.seek(0) # 再次将光标移动到开头
- f.seek(0, 2) # 将光标移动到结尾
- content2 = f.read() # 读取内容. 什么都没有
- print(content2)
f.seek(0) # 移动到开头- f.write("张国荣") # 写⼊信息. 此时光标在9 中⽂3 * 3个 = 9
print(f.tell()) # 光标位置9
f.flush()- f.close()
- f = open("⼩娃娃", mode="w", encoding="utf-8")
- f.write("哈哈") # 写⼊两个字符
- f.seek(3) # 光标移动到3, 也就是两个字中间
- f.truncate() # 删掉光标后⾯的所有内容
- f.close()
f = open("⼩娃娃", mode="r+", encoding="utf-8")- content = f.read(3) # 读取12个字符
- f.seek(4)
- print(f.tell())
- f.truncate() # 后⾯的所有内容全部都删掉
- # print(content)
- f.flush()
- f.close()
- # ⽂件修改
- import os
- with open("⼩娃娃", mode="r", encoding="utf-8") as f1,\
- open("⼩娃娃_new", mode="w", encoding="UTF-8") as f2:
- content = f1.read()
- new_content = content.replace("冰糖葫芦", "⼤⽩梨")
- f2.write(new_content)
- os.remove("⼩娃娃") # 删除源⽂件
- os.rename("⼩娃娃_new", "⼩娃娃") # 重命名新⽂件
- import os
- with open("⼩娃娃", mode="r", encoding="utf-8") as f1,\
- open("⼩娃娃_new", mode="w", encoding="UTF-8") as f2:
- for line in f1:
- new_line = line.replace("⼤⽩梨", "冰糖葫芦")
- f2.write(new_line)
- os.remove("⼩娃娃") # 删除源⽂件
- os.rename("⼩娃娃_new", "⼩娃娃") # 重命名新⽂件
作者:085iitirtu
链接:https://www.pythonheidong.com/blog/article/196550/82fee1d1b1c6c8bb761f/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!