代码:
for i in range(3):
if i ==0:
with open('learning_python.txt','r',encoding='utf-8') as files:
content=files.read()
print('one')
print(content)
if i ==1:
with open('learning_python.txt','r',encoding='utf-8') as files:
print('two')
print(content)
if i ==2:
with open('learning_python.txt','r',encoding='utf-8') as files:
content=files.readlines()
print('three')
print(content)
输出:
one
In Python you can 111111111
In Python you can 22222222
In Python you can 33333333333
two
In Python you can 111111111
In Python you can 22222222
In Python you can 33333333333
three
[‘In Python you can 111111111\n’, ‘In Python you can 22222222\n’, ‘In Python you can 33333333333\n’]
‘’’
程序判断当不等于n的时候,就执行。
while True:
name = input('请输入您的姓名:')
if name == 'n':
break;
with open('guest.txt','a+',encoding='utf-8') as files: #累加写的方式写入文件。
files.write(name)
files.write('\n')
files.seek(0)
content = files.read()
print(content)
请输入您的姓名:123456
joe
susan
anna
123456
请输入您的姓名: