2017-05-17 1 views
0

TL에 대해 검사 할 때와 올바른 입력을 인식하지 - 제목을 읽어파이썬은 다른 변수

내가 파이썬에 비교적 새로운 해요 젠장 - DR 더 구체적으로, 파일은 파이썬에서 처리 - 나는 뭔가 일하고 있어요 학교를 위해; 나는 파이썬 단독으로 시스템에 간단한 로그 (보안이나 아무것도, 단지 기본 프레임)를 만들려고 노력하고있다. 나는 이것에 대해 갈 가장 좋은 방법에 대해 궁금해했다. 내가 생각한 방법은 set directory에 set folder를 두는 것인데,이 폴더에는 그들이 저장 한 패스워드의 사용자 이름에 따라 이름이 지정된 파일들이 들어있다. (예를 들어 "jacksonJ.txt"는 그 사용자의 패스워드를 가지고있다.) 사용자는 사용자 이름을 입력하고, python은 그 파일을 가져오고, 암호를 읽은 다음, 사용자가 입력 한 암호를 실제 암호와 비교하여 검사합니다. 내 문제는; 올바른 암호가 입력 되더라도, 파이썬은 그것을 인식하지 못하는 것 같습니다. 문제는 당신이 파일에 대해 문자열을 비교 한 것이 었습니다 -

from pathlib import Path 
usr=input("Username: ") 

#creating a filepath to that user's password document 
filepath=("C:\\python_usr_database\\"+usr+".txt") 

#make sure that file actually exists 
check= Path(filepath) 

#if it does, let them enter their password, etc 
if check.is_file(): 

    #open their password file as a variable 
    with open (filepath, "r") as password: 
     pass_attempt=input("Password: ") 

     #check the two match 
     if pass_attempt==password: 
      print("Welcome back, sir!") 
     else: 
      print("BACK OFF YOU HACKER") 

#if it isn't an existing file, ask if they want to create that user 
else: 
    print("That user doesn't seem to exist yet.") 
    decision=input("Would you like to create an account? y/n ").lower() 
    # do some stuff here, this part isn't all too important yet 
+1

'password'는 내용이 아닌 파일입니다. 내용을 얻기 위해 파일을'.read()'해야합니다. – asongtoruin

+0

감사합니다! 내가 말했듯이, 나는 이것에 상당히 새로운 ... –

답변

0

당신은 당신이 그 내용을 얻기 위해 열 때 파일을 읽을 필요가있다. 다음을 시도하십시오.

from pathlib import Path 
usr=input("Username: ") 

#creating a filepath to that user's password document 
filepath=("C:\\python_usr_database\\"+usr+".txt") 

#make sure that file actually exists 
check= Path(filepath) 

#if it does, let them enter their password, etc 
if check.is_file(): 
    #open their password file as a variable 
    with open (filepath, "r") as f: 
     password = f.read() 
    pass_attempt=raw_input("Password: ") 

    #check the two match 
    if pass_attempt==password: 
     print("Welcome back, sir!") 
    else: 
     print("BACK OFF YOU HACKER") 
#if it isn't an existing file, ask if they want to create that user 
else: 
    print("That user doesn't seem to exist yet.") 
    decision=input("Would you like to create an account? y/n ").lower() 
    # do some stuff here, this part isn't all too important yet 
1

file.read()를 수행해야하는 파일의 내용을 가져 오려면 다음을 수행하십시오. 이것은 내용의 문자열을 반환합니다. So :

with open(filepath, "r") as password_file: 
    password = password_file.read() 
password_attempt = input("password: ") 
# Compare, then do stuff... 
+0

고마워! 이것은 효과가있다. –