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

2024-06-09 21:05:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在别处查,但没有结果。我该怎么做,比如让Python读取一个文件行,然后使用该行中的内容作为另一个文件的变量

本质上,我需要一个不同的文件作为验证密钥,当输入文件的内容(passkey)时,我的代码识别并传递它,然后打开所述文件。我还希望能够读取一个锁定文件,检查并查看用户是否应该被“锁定”,并需要输入密钥。有什么办法吗

更新:我自己编辑了一点代码,这样大家都知道了

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()

Tags: or文件thetxtinputrawifis
1条回答
网友
1楼 · 发布于 2024-06-09 21:05:17

this is where I want the code to read ".Verify.txt" and use its content as the passkey

我建议你从一个小得多的例子开始

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

类似地,您可以打开.Lockout.txt并检查其内容lockout

如果需要打开一个文件进行读/写,请使用"rw",而不是两个变量对同一个文件执行任何操作

相关问题 更多 >