f
=open('ly.txt','r',encoding
='utf-8')
data
=f
.read
()
print(data
)
f
.close
()
f
=open('test','a',encoding
='utf-8')
f
.write
("\n阿斯达所,\n天安门上太阳升")
f
.close
()
f
= open('ly.txt', 'r', encoding
='utf-8')
for i
in range(5):
print(f
.readline
().strip
())
for line
in f
.readlines
():
print(line
.strip
())
for index
,line
in enumerate(f
.readlines
()):
if index
==9:
print('----我是分隔符-----')
continue
print(line
.strip
())
count
=0
for line
in f
:
if count
==9:
print('----我是分隔符-----')
count
+= 1
continue
print(line
.strip
())
count
+= 1
f
= open('ly.txt', 'r', encoding
='utf-8')
print(f
.tell
())
print(f
.readline
(5))
print(f
.tell
())
f
.seek
(0)
print(f
.readline
(5))
print(f
.encoding
)
print(f
.buffer)
print(f
.fileno
())
print(f
.flush
())
import sys
,time
for i
in range(50):
sys
.stdout
.write
('#')
sys
.stdout
.flush
()
time
.sleep
(0.5)
f
= open('ly.txt', 'a', encoding
='utf-8')
f
.seek
(10)
f
.truncate
(20)
f
= open('ly.txt', 'r+', encoding
='utf-8')
print(f
.readline
().strip
())
print(f
.readline
().strip
())
print(f
.readline
().strip
())
f
.write
("我爱中华")
f
.close
()
f
=open("ly.txt","r",encoding
="utf-8")
f_new
=open("ly2.txt","w",encoding
="utf-8")
for line
in f
:
if "肆意的快乐" in line
:
line
=line
.replace
("肆意的快乐","肆意的happy")
f_new
.write
(line
)
f
.close
()
f_new
.close
()
import sys
f
=open("ly.txt","r",encoding
="utf-8")
f_new
=open("ly2.txt","w",encoding
="utf-8")
find_str
= sys
.argv
[1]
replace_str
= sys
.argv
[2]
for line
in f
:
if find_str
in line
:
line
=line
.replace
(find_str
,replace_str
)
f_new
.write
(line
)
f
.close
()
f_new
.close
()
with open('ly.txt','r',encoding
='utf-8') as f
:
for line
in f
:
print(line
.strip
())
with open('ly.txt','r',encoding
='utf-8') as f1
,open('ly2.txt','r',encoding
='utf-8'):
pass
output: 0
0
utf-8 <_io.BufferedReader name=‘ly.txt’> 4 None ##################################################
我爱中华
转载请注明原文地址: https://lol.8miu.com/read-19836.html