简体   繁体   English

如何让Python读取文件行并将其用作变量?

[英]How would I make Python read a file line and use it as a variable?

Tried looking it up elsewhere, to no avail. 试图在其他地方查找它,无济于事。 How would I do something like, having Python read a file line, then use what's in that line as a variable for a different file? 我该怎么做,让Python读取文件行,然后将该行中的内容用作另一个文件的变量?

Essentially, I want a different file that acts as a verification key, and when the content of the file (the passkey) is entered, my code recognizes that and passes it, then opening said file. 本质上,我想要一个不同的文件作为验证密钥,并且当输入文件的内容(密码)时,我的代码会识别出该文件并将其传递,然后打开该文件。 I also want to be able to read a lockout file to check and see whether the user should be "locked out", and need to enter the passkey. 我还希望能够读取锁定文件,以检查并查看用户是否应该“锁定”,并且需要输入密码。 Any possible way of doing this? 有任何可行的方法吗?

Update: I edited the code a little by my self, just so everyone is aware. 更新:我亲自编辑了一下代码,以便每个人都知道。

filename = ".UbuntuAlt/.Info.txt"
#I'm aware that the use of many of the "quit()" functions is ambiguous but idc
verify = ".UbuntuAlt/.Verify.txt"
locktxt = ".UbuntuAlt/.Lockout.txt"
#this is where I want to make ".Lockout.txt" verify whether the passkey needs to be used, and set variable "lockout" accordingly
infotxt = open(filename, "r")
verifyread = open(verify, "r")
locktestw = open(locktxt, "w")
locktestr = open(locktxt, "r")

if lockout == True:
    verify1 = raw_input("Please enter verification key: ")
    #this is where I want the code to read ".Verify.txt" and use its content as the passkey
    if verify1 == "look above":
        for line in infotxt:
            print line,
            infotxt.close()
            verifyread.close()
        lockout = False
        #this is where I want ".Lockout.txt" edited to be false-- I can do that myself though
        lockoutq = raw_input("Lockout is disabled. Reenable? [Y/n]: ")
        if lockoutq == "y" or "Y" or " ":
            #also where I plan on editing it
            quit()
        if lockoutq == "n" or "N":
            quit()
        else:
            lockdownerr = raw_input("Invalid input. [2] attempts remaining. Reenable? [Y/n]: ")
            if lockdownerr == "y" or "Y" or " ":
                #aaa
                quit()
            if lockdownerr == "n" or "N":
                quit()
            else:
                lockdownfinal = raw_input("Invalid input. [1] attempt remaining. Reenable? [Y/n]: ")
                if lockdownerr == "y" or "Y" or " ":
                    #aaa
                    quit()
                if lockdownerr == "n" or "N":
                    quit()
                else:
                    print "Invalid input. Enabling anyway."
                    #you get the point
                    quit()
    else:
        verifyread.close()
        print "You've inputted an invalid key. Aborting."
        quit()
else:
    for line in infotxt:
        print line,
        infotxt.close()
        verifyread.close()
    lockoutq2 = raw_input("Lockout is disabled. Reenable? [Y/n]: ")
    if lockoutq2 == "y" or "Y" or " ":
        #same as above w/ editing the lockout text
        quit()
    if lockoutq2 == "n" or "N":
        quit()
    else:
    lockdownerr = raw_input("Invalid input. [2] attempts remaining. Reenable? [Y/n]: ")
        if lockdownerr == "y" or "Y" or " ":
            #aaa
            quit()
        if lockdownerr == "n" or "N":
            quit()
        else:
            lockdownfinal = raw_input("Invalid input. [1] attempt remaining. Reenable? [Y/n]: ")
            if lockdownerr == "y" or "Y" or " ":
                #aaa
                quit()
            if lockdownerr == "n" or "N":
                quit()
            else:
                print "Invalid input. Enabling anyway."
                #you get the point
                quit()

this is where I want the code to read ".Verify.txt" and use its content as the passkey 这是我希望代码读取“ .Verify.txt”并将其内容用作密码的地方

I suggest you start with a much smaller example eg 我建议您从一个小得多的例子开始,例如

verify1 = raw_input("Please enter verification key: ")
passkey = open(".Verify.txt").read().strip()
if verify1 == passkey:
    print("Match")
else:
    print("Not Match")

Similarly, you can open .Lockout.txt and check its contents for lockout 同样,您可以打开.Lockout.txt并检查其内容以进行lockout

If you need to open a file for read/write, use "rw" , not two variables for doing either against the same file. 如果您需要打开一个文件进行读写操作,请使用"rw" ,而不要使用两个变量对同一个文件进行操作。

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

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