with open('password', 'r', encoding='utf-8') as f:
print(f.readlines())
for i in f.readlines():
if i == 'abc:cba':
break
else:
print('none')
这是password文件:
想起到的作用是for循环时,匹配到对应的值就跳出循环,但是每次都没法匹配到。
下图是输出结果
PHP中文网2017-04-18 10:25:58
你程式碼根本地方錯了 剛才我沒看清楚
with open('password', 'r', encoding='utf-8') as f:
print(f.readlines())
print(f.readlines())
第二次直接是 []
讀取檔案指標已經移動到底了 所以第二次沒內容了啊
with open('password', 'r', encoding='utf-8') as f:
# print(f.readlines())
# print(f.readlines())
readlines = f.readlines()
print(readlines)
for i in readlines:
if i.strip() == 'abc:cba':
break
else:
print('none')
這樣就可以了