Python Forum
Data is in and not in the file at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data is in and not in the file at the same time
#1
Hello everyone!

At the moment I'm working on a simple Regiter/Login project which uses text file to store all the data.

username=input("Please enter your username: ")
    with open("Data.txt", "r") as DataFile:
        for i,line in enumerate(DataFile):
            if username in line:
                print("Found the user in line:",i)
                data=line.split()
                password=data[1]
                print(password)
        if username not in DataFile:
            print("Username ",username," not found!")
I get no error messages, but the problem is that both if statements are correct- at the same time username is and not in the file. Any help would be appreciated.


The output:

Please enter your username: Jeff
Found the user in line: 1
123
Username Jeff not found!


DataFile:

Jeff 123
Sandra 753
Mike 482
Reply
#2
You are checking if username is in the entire line...which it is. You should split the data first and compare it only to the first element (since the username is always first)
    for i,line in enumerate(DataFile):
        data=line.split()
        if username in data[0]:
            password = data=[1]
And for the second if clause you could use a boolean to compare between all loops.
username=input("Please enter your username: ")
found = False
with open("Data.txt", "r") as DataFile:
    for i,line in enumerate(DataFile):
        data=line.split()
        if username in data[0]:
            password = data=[1]
            found = True
            
if not found:
    print("Username {} not found!".format(username))
Or you could use a function to return found boolean
username=input("Please enter your username: ")
with open("Data.txt", "r") as DataFile:
    lines = DataFile.readlines()
    
def user_found(username, lines):
    for line in lines:
        data=line.split()
        if username in data[0]:
            return True

if not user_found(username, lines):
    print("Username ",username," not found!")
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Real time data satyanarayana 3 20,195 Feb-16-2022, 07:46 AM
Last Post: satyanarayana
  How to read rainfall time series and insert missing data points MadsM 4 2,123 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Check last time file was accessed Pavel_47 4 2,759 Jun-01-2021, 05:47 PM
Last Post: Yoriz
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,195 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 2,187 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Naming the file as time and date. BettyTurnips 3 2,887 Jan-15-2021, 07:52 AM
Last Post: BettyTurnips
  xml file creation from an XML file template and data from an excel file naji_python 1 2,069 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  how to change the range of read CSV file every time python file runs greenpine 6 4,379 Dec-08-2020, 10:11 PM
Last Post: greenpine
  ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' rajesh3383 4 14,195 Sep-03-2020, 08:22 PM
Last Post: buran
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,251 Aug-17-2020, 03:44 PM
Last Post: bearcats6001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020