Steven Tautonico
Steven Tautonico

Reputation: 165

How can I check if a line in a file has information or not in Python 3.6?

I am trying to create a memory set, saved in a separate text file stored in a different location. I have tried to use some of the suggestions on this site, but maybe my code is different. I am using Python 3.6. Here is my code:

df = open("Memory.txt", 'r')
askforfname = df.readlines()
df.close()
print (askforfname)
if '\n' in open("Memory.txt", 'r').readlines():
    df.close()
    name = input("What is your name?: \n")
    Bigname = name.title()
    df = open("Memory.txt", 'w')
    df.write(Bigname)
    df.close()

else:
    df = open("Memory.txt", 'r')
    returning_name = df.readlines()
    print("Welcome back" + str(returning_name))
    df.close()

I am using print(askforfname) in the fourth line just to make sure the data is being read from the proper line, and it is. But it just skips over the if command, as if it came out false, and continues right the the else command, even if the line is blank. I am just a beginner in programming, and don't know much, so i don't know if I'm just making a simple mistake lol. And yes, I understand my code must be inefficient, but since I am new, this is just me experimenting with code.

Upvotes: 1

Views: 618

Answers (4)

Jieshao
Jieshao

Reputation: 31

First, there must be something in Memory.txt. '\n' indicates a blank line in the file. There is already a very clear example, I'm giving you some tips:

  1. If the file is large, you can read multiple times, limiting the size of the file each time it is read to ensure it does not take up too much memory:
 name = Jack
 with open('Memory.txt', 'r') as f:
     while True:
         content = f.read(2048)
         if content == '':
             break
         else:
             if name.lower() in content:
                 print('name is here')
             else:
                 print('name is not here')
  1. If this file is small, the memory can hold the contents of the file, you can use readlines() and read().

Upvotes: 0

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

Yes, inefficient, but also harder to read and understand with repeated opening and line reading. Please to not post such code.

.readlines produces a list of strings representing lines, terminated by \n if the line in the file is so terminated. Your test is correct, if obscured.

>>> '\n' in ['']
False
>>> '\n' in ['a']
False
>>> '\n' in ['a\n']
False
>>> '\n' in ['\n']
True

It must be that your file does not contain a blank line ending with a newline.

Upvotes: 1

adder
adder

Reputation: 3698

Let names.txt be a simple .txt file with a few names in it:

ken
dennis
guido
bjarne

Say you're Bjarne and want to check whether your name is in the file. Probably the most efficient approach would be not to read the file into memory at all, but rather lazily get its contents. Apart from that, you would be better off using with context manager, instead of manually managing closing your file objects like that:

myself = 'Bjarne'

with open('names.txt', 'r') as f:
    for line in f:
        if myself.lower() in line:
            print('Here am I!')
        else:
            print('No, this is not me')

Upvotes: 1

Safwan
Safwan

Reputation: 3428

Your Memory.txt file is an empty file and the result of if condition will be False. You need to have at least a blank line in your file for if condition to return True.

Upvotes: 1

Related Questions