问题是代码除了正确的密码外,我似乎无法使其正常工作并且已经尝试了一段时间。虽然代码可能有效,但我尝试了许多方法,但到目前为止还没有。
代码:
import os
PassCount = 0
SetUp = 0
newpath = r"PassEncryptPY"
if not os.path.exists(newpath):
os.makedirs(newpath)
if not os.path.exists("PassEncryptPY/PassEncryptPY_PTF.txt"):
with open("PassEncryptPY/PassEncryptPY_PTF.txt", "x"): pass
if not os.path.exists("PassEncryptPY/PassEncryptPY_ETF.txt"):
with open("PassEncryptPY/PassEncryptPY_ETF.txt", "x"): pass
f = open("PassEncryptPY/PassEncryptPY_PTF.txt", "r+")
g = open("PassEncryptPY/PassEncryptPY_ETF.txt", "r+")
if os.path.getsize(r"PassEncryptPY/PassEncryptPY_PTF.txt") == 0:
print("we have detected that you dont have a password")
pas = input("Your password: ")
f.write(pas)
SetUp = SetUp + 1
if os.path.getsize(r"PassEncryptPY/PassEncryptPY_ETF.txt") == 0:
print("we have detected that you dont have a email")
ema = input("Your email: ")
g.write(ema)
SetUp = SetUp + 1
if SetUp != 0:
print("Set Up complete, restarting")
f.flush()
g.flush()
os.fsync(f.fileno())
os.fsync(g.fileno())
#f.close()
#g.close()
#import sys
#os.execl(sys.executable, sys.executable, * sys.argv)
def Main():
if PassCount < 3:
inptpass = input("Please insert your password: ")
if inptpass == f.read():
print("test done")
else:
print("nope")
Main()
f.close()
g.close()
输出:
we have detected that you dont have a password
Your password: Pas
we have detected that you dont have a email
Your email: Ema
Set Up complete, restarting
Please insert your password: Pas
nope
任何帮助表示赞赏。
答案 0 :(得分:0)
在f.seek(0)
功能中使用Main()
将光标重置在文件的开头。
def Main():
if PassCount < 3:
f.seek(0)
inptpass = input("Please insert your password: ")
if inptpass == f.read():
print("test done")
else:
print("nope")
当您使用read()
或write()
功能时(当密码和电子邮件未存储在文件中时,您在Main()
之外执行此操作)read()
或write()
会将光标留在文件末尾。因此,第二次尝试从文件中读取数据时,光标将位于文件的末尾。
另一种选择是关闭文件并重新打开它。
使用read()
证明:
def Main():
if PassCount < 3:
inptpass = input("Please insert your password: ")
print(f.read())
print(f.read())
f.seek(0)
print(f.read())
if inptpass == f.read():
print("test done")
else:
print("nope")
这是输出:
Please insert your password: 123
123
123
nope