[英]Read text file data Python 2.7
I'm trying to make an activation method for my Python program where it reads a text file.我正在尝试为我的 Python 程序创建一个激活方法,它读取文本文件。 If it reads 1, the person should be able to use the program without activation.
如果显示为 1,则此人应该无需激活即可使用该程序。 But when I do it, it either doesn't run, or keeps saying 'Enter product key: '.
但是当我这样做时,它要么不运行,要么一直说“输入产品密钥:”。 Here's what I've done so far...
这是我到目前为止所做的......
fff1 = open("savefile.txt", "w+")
fff1.close()
fff = open("savefile.txt", "r")
if fff.mode == 'r':
content1 = fff.read()
print fff.read()
fff.close()
if content1 == "1":
while True:
terminal()
else:
Prodkey = ''
print ''
print 'Enter product key:'
Prodkey = ''
while True:
d = msvcrt.getch()
if d == '\r':
break;
sys.stdout.write('=')
Prodkey += d
if Prodkey == '123321':
f = open("savefile.txt", "w+")
f.write("1")
f.close()
ff = open("savefile.txt", "r")
if ff.mode == "r":
contents = ff.read()
ff.close()
product = contents
if product == "1":
print ''
while True:
terminal()
else:
print "Incorrect product key."
terminal()
Can anyone figure out this kink?任何人都可以弄清楚这个扭结吗?
Try this:尝试这个:
import os
from getpass import getpass
def check_activation(file_path):
if os.path.exists(file_path):
with open(file_path, "r") as f:
return f.read(1) == "1"
return False
def main():
file_path = "savefile.txt"
if check_activation(file_path):
# this product is activated, continue your logic here
pass
else:
# Try to activate the product
prod_key = getpass('Enter product key: ')
if prod_key == '123321':
with open(file_path, "w") as f:
f.write("1")
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.