简体   繁体   中英

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. If it reads 1, the person should be able to use the program without activation. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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