简体   繁体   English

Python 3 readline()不起作用

[英]Python 3 readline() does not work

The Text File: 文本文件:

username
password
other

The code: 编码:

filepass = datafile.readline(1)
filepass.rstrip('\n')
datafile.close()
entered_password = input("Enter Password")
if entered_password == filepass:
    print ("Success")
else:
    print ("Failure")

Python always says "Failure" no matter what I type in. What do I need to do? 不管我输入什么内容,Python都总是说“ Failure”。我需要做什么? Thanks in Advance. 提前致谢。

  1. Your question title states readlines() , your question code uses readline() . 您的问题标题为readlines() ,您的问题代码使用readline() Two different functions 两种不同的功能
  2. The size / hint parameter you have specified reads up to the number of bytes (not lines in either case -- bytes). 您指定的size / hint参数最多读取字节数(在任何情况下都不行-字节)。

You probably meant something like this 你可能是这样说的

entered_password = datafile.readlines()[1].strip()

With some obvious fault tolerance to add to your code, but not specified here. 可以在代码中添加一些明显的容错功能,但此处未指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM