我们首先假设已有一个flag.txt文件
方法一:
for line in open("flag.txt"):
print(line)
运行结果:
方法二:
f = open("flag.txt","r")
lines = f.readlines()#读取全部内容
for line in lines:
print (line)
运行结果:
搞定~~~
在这里拓展一下,我想对flag.txt的文件进行修改,让他输出结果显示为:
aaa
aab
aac
aad
aae
aba
abb
abc
abd
abe
…
代码:
strs=["a","b","c","d","e"]
for line in open("flag.txt"):
for i in strs:
print(line.rstrip()+i)
运行结果: