2012-05-04 41 views
-3
def info(): #Here you can write your password and username. 
Username = raw_input ("Username: ") 
Password = raw_input ("Password: ") 
print("") 
for line in open('/home/hello/Usernames.txt'): 
    if Username == Username in line: #Checks if username is available. 
    print ("Username is already taken!\n") 
    info() 
    else: 
    User = open("/home/hello/Usernames.txt", "w") #Registers username. 
    User.write(Username) 
    Psw = open("/home/hello/Passwords.txt", "w") #Registers password. 
    Psw.write(Password) 
    print ("You have succsesfully registered!") #If you managed to register. 
    break 

info() 

這是一個帳戶註冊器,可以註冊用戶名和密碼。但是我需要某些幫助......如何在一個文件中檢查多行字符串,以及在註冊時如何讓程序在文本文件中寫入一行新字符串而不替換舊字符串?在python中搜索和寫入多個字符串的行?

回答

1

打開文件以附加('a')模式而不是寫入文件('w')來截斷文件。

+0

我試過了你的提示,它只在第一個用戶名下注冊了一行,當我註冊第三個用戶名和第四個用戶名。然後它只是寫在與用戶名2相同的行上。以下是發生了什麼:我已經註冊了名爲「username2」的用戶名二,然後我註冊了名爲「username3」的第三個用戶名。當程序將「username3」寫入文本文件時,它不會排隊。結果變成「username2username3」 – Tobold

+0

你需要自己寫新行到文件中:'User.write(Username +「\ n」)' – alexis