Sparxes
Sparxes

Reputation: 25

Read only specific lines

I only want to read certain lines, for example lines in the range from 0 to 10 and the program should check the correctness of the data entered by the user.

from ftplib import FTP
from ftplib import error_perm

start = 0 # some starting index
end = 5

with open('string.txt', 'rb') as password_list:
     for i,line in enumerate(password_list):
       if(i>start & i<end):
        line=line.replace(b'\n',b'').replace(b'\r',b'')
        password=line.decode("utf-8")

Upvotes: 2

Views: 37

Answers (1)

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Try this :

start = 0
end = 5
with open('string.txt', 'rb') as fh:
   lines = [i.replace(b'\n',b'').replace(b'\r',b'') for i in fh.readlines()][start:end]
   passwords = [i.decode('utf-8') for i in lines]

Upvotes: 1

Related Questions