解决f.write问题。如何在f.write()中编辑一行?

时间:2019-02-14 08:33:54

标签: python

我目前正在制作一个简单的角色系统。角色应类似于:成员,主持人,管理员。问题是昨天,当我编辑代码时,TXT文件中的所有行都以某种方式删除了。这有什么问题?现在在使用f.write()的Register / Login系统中,添加新帐户后什么也没有发生。当我尝试使用下面的代码制作角色系统时,所有问题都开始了。如何只编辑一行? 代码:

def Password():
    global Password
    return Password

def Ban():
    global Ban
    Ban = False
    return Ban

def RegTime():
    global RegTime
    RegTime = strftime("%Y-%m-%d %H:%M", gmtime())
    return RegTime

def Role():
    global Role
    Role = 'Member'
    return Role

check = True
while check:
    Register_Login = input("~ Welcome, LOGIN L, REGISTER R.\n")
    if "r" in Register_Login or "R" in Register_Login:
        with open(Database, mode='a', encoding='utf-8') as f:
            Vardas = input("~ Please pick a username for you Account!\n").lower()
            Password = input("~ Please enter your Password!\n")
            if " " in Vardas or " " in Password or len(Vardas) < 5 or len(Password) < 3 :
                print(" Cannot Contain null!")
                continue
            else:
                with open(Database, mode='r', encoding = 'utf-8') as ac:
                    for line in ac:
                        if Vardas in ac.read():
                            print("Sorry Username is already taken!")
                            check = True
                            continue
                        else:
                            Gmail = input("~ Please add a Gmail for your account\n").lower()
                            if " " in Gmail or len(Gmail) < 7 :
                                print("Cannot Contain null!")
                                continue
                            else:
                                #elif " @gmail.com" in Gmail and len(Gmail) < 7 :
                                # Setting up New account. Options Roles.
                                RegTime()
                                Ban()
                                Role = 'Member'
                                BanReason = 'None.'
                                print("Account Created!")
                                f.write(f"Vardas : {Vardas} Password : {Password} Gmail: {Gmail} Ballance : {Kreditas.get_balance()} BAN : {Ban} BAN Reason: {BanReason} Role: {Role} RegTime : {RegTime}\n")
                                f.close()
                                check = False
                                break
    elif "l" in Register_Login or "L" in Register_Login:
        while check:
            with open(Database, mode = 'r', encoding = 'utf-8') as f:
                    Vardas = input("Enter your Username!\n").lower()
                    Password = input("Please enter Your password!\n")
                    for line in f:
                        if Vardas + " Password : " + Password + " " in line.strip():
                            clear()
                            print("~ You're logged in as {}".format(Vardas))
                            check = False
                            f.close()
                            break
                        else:
                            clear()
                            print("Wrong password!")
                            check = True
                            continue

print(" Username - {} Password- {}, Role - {} Your Ballance - {} EUR, ".format(Vardas, Password, Role, Kreditas.get_balance()))


while True:
    if Role == 'Member':
        BanOrNo = input("~ There's some people that have reports may you want to ban some? (Y,N)\n").lower()
        if "y" in BanOrNo:
            Pasirinkimas_BAN = input("~ Please enter a Username which would get BAN\n").lower()
            Ban_Priezastis = input("~ Ban reason:\n")
            BanReason = Ban_Priezastis
            with open(Database, mode = 'a', encoding = 'utf-8') as BanArchive:
                for line in BanArchive:
                    if "Vardas : " + Pasirinkimas_BAN + " Password : " in line.strip():
                        print("~ Would you like to Ban {} ?".format(Pasirinkimas_BAN))
                        print("Ban Reason : {}".format(Ban_Priezastis))
                        Confirmas_Bano = input("~ Please Confirm: (Y,N)\n").lower()
                        if "y" in Confirmas_Bano:
                            clear()
                            print("User {} was Banned by {} !\n Ban Reason: {} ".format(Pasirinkimas_BAN, Vardas, Ban_Priezastis))
                                # Make a edit on the file section with open (database, ? , utf-8) as fn:
                                # for line in fn:
                                # where line Ban : Change it to True, BanReason : + Ban_Priezastis
                            f.close()

                        elif "n" in Confirmas_Bano:
                            break    
        elif "n" in BanOrNo:
            break        
    elif Role == 'Admin':
        break

**编辑 尝试注册帐户后,会发生这种情况。这是从终端。不管您键入什么,布尔都会继续播放。

~ Welcome, LOGIN L, REGISTER R.
L
Enter your Username!
  jurgis
Please enter Your password!
  jurgis
Enter your Username!
  jurgiiis
Please enter Your password!
  gagsa
Enter your Username!
Terminated

0 个答案:

没有答案