John Clarkson
John Clarkson

Reputation: 23

How to make python code read a certain line of text in a .txt file?

So i have been fiddling around with python and league of legends. And i found out you can make notes in game. So i thought about making python code read a line of text from the note i made in game, like "Lux no flash", but it doesn't seems to be able to read it at all, it only works when i do it manually with the exact same code. Here is my code:

import os
import time
def main():
    os.chdir('C:\\Riot Games\\League of Legends\\RADS\\solutions\\lol_game_client_sln\\releases\\0.0.1.237')
    f=open("MyNotes.txt", "r")
    if f.mode == 'r':
        lines=f.readlines()
        text = lines[4]
        time.sleep(0.1)
        if text == 'Lux no flash':
           print('Done')
        else:
            print('Something went wrong')
    f.close()
if __name__== "__main__":
 main()

The output is "something went wrong", but when i do it manually it says "done". I feel like python cant read league code. Maybe you guys know how to do this... This is the .txt file im trying to access:

##################################################
2018-09-13_18-57-33_
##################################################
Lux no flash

Upvotes: 0

Views: 174

Answers (2)

vash_the_stampede
vash_the_stampede

Reputation: 4606

Using lux.txt:

##################################################
2018-09-13_18-57-33_
##################################################
Lux no flash

Code:

content = []
with open('lux.txt', 'r') as f:
    for line in f:
        content.append(line.strip('\n'))

    for i in content:
        if 'Lux no flash' == i:
            print("Done")
        else:
            pass

Better @pygo

with open('lux.txt', 'r') as f:
    content = f.read()
    if 'Lux no flash' in content:
        print("Done")
    else:
        print("No else, this works :)") 

Output:

(xenial)vash@localhost:~/python/stack_overflow$ python3.7 lux.py
Done

Upvotes: 2

Karn Kumar
Karn Kumar

Reputation: 8826

I'm Just taking a file on an assumption basis:

# cat MyNotes.txt
there is Lux no flash in line
there is Something went wrong
There is nothing lux no flash
this is all test

So, just looking for the word 'Lux no flash' you are searching into your file, we can simply do as below.. but its case sensitive.

It's always best practice to use with open() method to read a file.

import os
import time
def main():
       with open("MyNotes.txt") as f:
        for line in f.readlines():
            if 'Lux no flash' in line:
                print('Done')
            else:
                print('Something went wrong')
if __name__== "__main__":
    main()

Output result will be :

Done
Something went wrong
Something went wrong
Something went wrong

Even tried using the lux.txt , it works as expected with my code.

import os
import time
def main():
    with open("lux.txt") as f:
        for line in f.readlines():
            #line = line.strip()  # use can use the strip() or strip("\n")
            #line = line.strip("\n") # if you see white spaces in the file
            if 'Lux no flash' in line:
                print('Done')
            else:
                pass
if __name__== "__main__":
    main()

Resulted outout is:

# test.py
Done

Upvotes: 2

Related Questions