2012-02-11 2 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의 파일이 암호 조합을 그 나는이 객체 을 "삭제"하고 싶은하지만이삭제 사용자 이름이 아닌 텍스트 파일

if delete in username: 

인수를 사용하는 경우 문제가있다, 그것은뿐만 아니라 암호를 생각해야합니다. 예를 들어, 같은 암호를 가진 두 개의 계정이 있다면 어떻게합니까? 또는 위의 것과 같습니다. 이 경로로 어떤 경로를 취할 수 있습니까? 아니면 여기에 뭔가 빠졌나요?

+0

이것은 좋지 않습니다. 암호화 된 방식으로 또는 올바른 권한을 가진 DB에 암호를 암기하는 것을 생각하십시오. – DonCallisto

+0

어떻게 할 수 있습니까? 죄송합니다. 저는 방금 6 개월 미만의 기간 동안 Python에 있었으며 지식도 제한되어 있습니다. –

+0

이것은 파이썬 문제가 아니라 보안입니다. 약 읽으십시오. G '에서 당신은 모든 것을 찾을 수 – DonCallisto

답변

0

:


당신은 파일 "인플레 이스"를 재 작성하기 위해 fileinput 모듈을 사용할 수 있습니다 삭제 및 작성 :

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

오! 내가보기에, 나는 이것을 얻는다. :) thanks :) 이것은 나의 코드를 더 짧게한다. :) –

0

사용

if delete in Guest: 

delete 경우 테스트 할 수는 Guest의 핵심이다. Guest의 키는 사용자 이름을 나타내므로 if delete in Guestdelete이 사용자 이름인지 테스트합니다. 사용자에게로 시작하는 줄을 제거, 당신은 파일을 읽을 수있는 현재 DelUser 기능에 따라

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

을 보시오. 그러나 제거하고 싶습니다. 게스트 dict뿐만 아니라 텍스트 파일의 사용자 이름 어떤 제안? –

관련 문제