标签:修改 文件 import open 位置 python sys 内存 ace
创建一个文件名字为filename
内容是
BJ
SH
GD
TW
f = open("filename",‘r‘,encoding="utf-8")
print(f.encoding)
utf-8
print(f.fileno())
3
f = open("filename",encoding="utf-8")
data = f.read()
print(data)
BJ
SH
GD
TW
f = open("filename",encoding="utf-8")
for i in range(2):
print(f.readline().strip())
BJ
SH
f = open("filename",encoding="utf-8")
for i in f:
print(i.strip())
BJ
SH
GD
TW
count = 0
f = open("filename",encoding="utf-8")
for i in f:
if count == 3:
print(‘----------------‘)
count += 1
print(i.strip())
count += 1
BJ
SH
GD
----------------
TW
ssssss
f = open("filename2",‘w‘,encoding="utf-8")
f.write("wwwww")
f.close()
f1 = open("filename2",encoding="utf-8")
data = f1.read()
print(data)
f.close()
wwwww
在文件尾部追加
f = open("filename",‘a‘,encoding="utf-8")
f.write("\nssssss")
f.close()
BJ
SH
GD
TW
ssssss
f = open("filename","r+",encoding="utf-8")
f.write("\n------haha------------")
for i in f:
print(i.strip())
f = open("filename","r",encoding="utf-8")
f1 = open("filename1","w",encoding="utf-8")
for line in f:
if "h" in line:
line = line.replace(‘h‘,‘g‘)
f1.write(line)
f.close()
f1.close()
f = open("filename",encoding="utf-8")
print(f.tell()) #查看当前指针位置
print(f.readline())
print(f.tell())
f.seek(0) #指针回到0
print(f.tell())
print(f.readline())
print(f.tell())
0
BJ
4
0
BJ
4
f = open("filename",‘r‘,encoding="utf-8")
print(f.readable())
print(f.seekable())
print(f.writable())
True
True
False
f = open("filename",‘r‘,encoding="utf-8")
print(f.flush())#从内存写入磁盘
print(f.buffer)
None
<_io.BufferedReader name=‘filename‘>
import sys,time
for i in range(10):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.5)
##########
f = open("filename","a",encoding="utf-8")
f.seek(0) #指定指针到0的位置
f.write("123456")
f.truncate(2) #截断2字符
filename文件中 只保留2个字符
BJ
标签:修改 文件 import open 位置 python sys 内存 ace
原文地址:https://www.cnblogs.com/wsy1030/p/9013068.html