2012-02-11 11 views
0
Guest = {} 
with open('LogIn.txt') as f: 
    credentials = [x.strip().split(':') for x in f.readlines()] 
    for username,password in credentials: 
     Guest[username] = password 
def DelUser(): 
    DB = open('LogIn.txt',"r+") 
    username = DB.read() 
    delete = raw_input("Input username to delete: ") 
    if delete in username: 
     <insert code to remove line containing username:password combination> 

、私は次のユーザ名とLogIn.txtファイルがあります。パスワードの組み合わせ:削除したユーザー名とない

chris:test 
char:coal 
yeah:men 
test:test 
harhar:lololol 

私はユーザー名を削除する:パスワードの組み合わせ私がオブジェクトに を「削除」したいしかし、私は

if delete in username: 

引数を使用する場合の問題は、ある、それは同様に、パスワードを検討する必要があります。たとえば、同じパスワードを持つアカウントが2つある場合はどうなりますか?または上記のように。この道ではどのような道を取ることができますか?または私はここに何かを逃していますか?

+0

これはお勧めできません。暗号化された方法で、または適切な権限を持つDBにパスワードを記憶させることを考えてください。 – DonCallisto

+0

どうすればいいですか?申し訳ありませんが、私はちょうど6ヶ月以内にPythonにいて、知識も限られています。 –

+0

これはpythonの問題ではなく、セキュリティです。何かについて読む。 Gであなたはすべてを見つけることができます – DonCallisto

答えて

0

現在DELUSER機能によると、あなたがファイルを読むことができる、にユーザーで始まる行を削除削除、書き込み:

def DelUser(): 

    # read the current files, and get one line per user/password 
    with open('LogIn.txt',"r+") as fd: 
     lines = fd.readlines() 

    # ask the user which one he want to delete 
    delete = raw_input("Input username to delete: ") 

    # filter the lines without the line starting by the "user:" 
    lines = [x for x in lines if not x.startswith('%s:' % delete)] 

    # write the final file 
    with open('LogIn.txt', 'w') as fd: 
     fd.writelines(lines) 
+0

私はこれを得ることができない。説明する気に? –

+0

ああ!私は見る、私はこれを今得ます:)感謝:)これは私のコードをより短くします。 :) –

0

使用

if delete in Guest: 

deleteGuestに重要であるかどうかをテストします。 Guestのキーはユーザー名を表しますので、if delete in Guestdeleteがユーザー名かどうかをテストします。


あなたはファイル「インプレース」を書き換えることfileinputモジュールを使用することができます。

import fileinput 
import sys 

def DelUser(Guest): 
    delete = raw_input("Input username to delete: ") 
    for line in fileinput.input(['LogIn.txt'], inplace = True, backup = '.bak'): 
     if delete not in Guest: 
      sys.stdout.write(line) 
+0

を参照してください。 Guest dictの中だけでなく、テキストファイルからのユーザー名。助言がありますか? –

関連する問題