2017-09-10 33 views
0

試圖通過Python創建一個登錄工人 當我得到一個簡單但令人困惑的錯誤時。得到錯誤讀取文件

這裏是我得到的錯誤。

Traceback (most recent call last): 
    File "stratixlogin.py", line 87, in <module> 
    main() 
    File "stratixlogin.py", line 78, in main 
    login_worker() 
    File "stratixlogin.py", line 51, in login_worker 
    data = f.read() 
ValueError: Mixing iteration and read methods would lose datanter code 

這裏就是錯誤的現象發生:

with open("global_users.txt", "r") as f: 
    for line in f: 
     data = f.read() 
     if data == username_ask: 
      print(G+"Success!") 

      password_ask = raw_input(O+"Password:"+W+" ") 

      with open("global_passwords.txt", "r") as f: 
       for line in f: 
        data = f.read() 
        if data == password_ask: 
         print(G+"Success!") 

        else: 
         print(R+"Incorrect Password!") 

     else: 
      print(R+"No Users Found!") 

我不知道的錯誤做法是什麼,但我對如何解決這一困惑。有任何想法嗎?

回答

0

您無法通過文件(for迴路)和read()的線路混合迭代。
這就夠了:

with open("global_users.txt", "r") as f: 
    for data in f: 
     if data == username_ask: 
      print(G+"Success!") 

      password_ask = raw_input(O+"Password:"+W+" ") 

      with open("global_passwords.txt", "r") as f: 
       for line in f: 
        data = f.read() 
        if data == password_ask: 
         print(G+"Success!") 

        else: 
         print(R+"Incorrect Password!") 

     else: 
      print(R+"No Users Found!") 
+0

好的,謝謝!我現在只需修復我的新發生的錯誤:)感謝幫助的人! –