0

So basically my code grabs a line from my txt and does its job with selenium. the problem is after every check, my program should give me the status after the selenium check and the line that has been checked. the problem is both of the functions are in different def's. the selenium part and the check status part.

so i could do x = f.readline() and call it after the check is done which will work with no issues tho because the x is needed in the 1. def and it's changing every time, i can't change his position. so i tried to do y = f.readline() in the status def, which should also work but because i call readline again, both of the variables change. i need it to check and report line by line. Also readline() Copies \n which is causing a lot of trouble

Let me make it a little bit more clear. here is the more simple code

f = open('list.txt', 'r')

def part1():
    x = f.readline()
    xxx = x[0:][:16]
    xxxx = x[17:][:2]
    driver.get('https://website.com')
    elem = driver.find_element_by_name("example")
    elem.send_keys(xxx);
    elem = driver.find_element_by_name("example2")
    elem.send_keys(xxxx);
    ......

def checkpart():
    part1()
    (some sleep code)
    if driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess')]"):
        print('[-] Bad: ', x)
    elif driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess2')]"):
        print('[+] Good: ', x)
    else:
        print('[+] Smthelse: ', x)

(some input questions...)

i = 1

register() //other function thats not needed here//

while i <= input:
        checkpart()
        time.sleep(1)
        i += 1

Its a bit complicated and i couldnt found an easier way of readline(). tho it doesnt allow me to display x in the checkpart and it also copies \n

I need to display the current checked line (which is x) without changing it and without copying \n in the end of it.

As i said its a bit complicated for me i've made a lot of research tho didnt come up with anything that will help. i also issue the same in php.

  • Does this answer your question? [Getting rid of \n when using .readlines()](https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines) – Vikas Mulaje Apr 20 '20 at 13:37

4 Answers4

0

Instead of using the filehandle itself (variable f), you could read all text into memory and work from there. You won't have to worry about the positioning of the cursor in your file stream.

f = open('list.txt', 'r')
content = f.readlines()
f.close()

for line in content:
    part1(line)
    checkpart(line)

Now you can work with the variable content without a headache

DerHamm
  • 173
  • 8
  • and how to input line by line. as i said i have to input the lines to selenium part to be checked. also i have to display it on the other def. also is this going to remove the \n's? – Jivareng Apr 20 '20 at 13:51
  • Assuming, that content is a list of lines of your list.txt, you could iterate over this list and call both functions within the loop. The first function doesn't need a readline() anymore, you can pass the current entry as parameter to both functions. Edit: I adapted my example to this comment – DerHamm Apr 20 '20 at 13:54
0

You could do the filereading in checkpart() and simply pass x onto part1(x). And to avoid the trailing newline, you could use x.rstrip()

f = open('list.txt', 'r')

def part1(x):
    xxx = x[0:][:16]
    xxxx = x[17:][:2]
    driver.get('https://website.com')
    elem = driver.find_element_by_name("example")
    elem.send_keys(xxx);
    elem = driver.find_element_by_name("example2")
    elem.send_keys(xxxx);
    ......

def checkpart():
    x = f.readline().rstrip()
    part1(x)
    (some sleep code)
    if driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess')]"):
        print('[-] Bad: ', x)
    elif driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess2')]"):
        print('[+] Good: ', x)
    else:
        print('[+] Smthelse: ', x)

(some input questions...)

i = 1

register() //other function thats not needed here//

while i <= input:
        checkpart()
        time.sleep(1)
        i += 1
Sri
  • 2,281
  • 2
  • 17
  • 24
  • im sorry its a real good advise but how to pass it onto part1. also part1 needs to use the variable first. im new in python – Jivareng Apr 20 '20 at 13:53
0

Have you tried reading the file? You can then use split to create or list for example:

f = open("list.txt", "r")

f_read = f.read()

f_list = f_read.split("\n")
Jonath P
  • 519
  • 5
  • 16
  • and how to input line by line to the selenium def and output the same in the check def – Jivareng Apr 20 '20 at 13:52
  • I don't know what this selenium is, but once you have the list, you can just iterate through it and do whatever you want with it. – Jonath P Apr 20 '20 at 13:54
0

May print(x, end = "") will be sufficient?

Could you give additional requirements?

Alternatively you can strip just last character with x[:-1], but as I observe, you are already aware of this possibility.

--- EDIT --- BTW, as mentioned above, it probably is better to read whole file (or portion) to list, and then iterate list.

geoai777
  • 102
  • 1
  • 8