文件
In python you can do something very boring!
In python you can make your own game!
In python you can find a good job!
In python you can finish your homework easy!
'''读取文件,并保存在pi_string中'''
filename = 'learning_python.txt'
with open(filename) as file_object:
lines = file_object.readlines()
'''打印文章内容三次'''
print('\n第一次打印 读取整个文件')
print(lines)
print('\n第二次打印 遍历文件对象')
for line in lines:
print(line)
print('\n第三次打印 在with外打印')
pi_string = ' '
for line in lines:
pi_string += line.lstrip()
print(pi_string)
结果
第一次打印 读取整个文件
['In python you can do something very boring!\n', 'In python you can make your own game!\n', 'In python you can find a good job!\n', 'In python you can finish your homework easy!']第二次打印 遍历文件对象
In python you can do something very boring!In python you can make your own game!
In python you can find a good job!
In python you can finish your homework easy!
第三次打印 在with外打印
In python you can do something very boring!
In python you can make your own game!
In python you can find a good job!
In python you can finish your homework easy!Process finished with exit code 0