G
G
Gitpy2019-07-11 11:20:49
Python
Gitpy, 2019-07-11 11:20:49

When reading, one empty line is added, which leads to an error, what should I do?

Wrote a program to work with ftp. If there is 1 line in the file, then everything is fine, but if there are 2 or more lines, then a line is read and one empty line is added to it, which leads to an error when connecting to ftp.
Program code:

user="admin"
passw="admin"
from ftplib import FTP_TLS
f = open('ip.txt')
for ip in f:
    print(ip)
    ftp = FTP_TLS(ip)
    if "331" in ftp.sendcmd('USER '+user):
        try:
            ftp.sendcmd('PASS '+passw)
        except:
            print("530 Password or login is bad")
        else:
            print("230 Password is good")

File ip.txt:
ftp.ihostfull.com
ftp.djejjsjrhjsks.by
ftp.debian.org

Here is what outputs to the console:
ftp.ihostfull.com
Please tell me the solution.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-07-11
@dmtrrr

Check if there is a line break ('\n') at the end of the line and remove it if there is one.

M
mactep3230, 2019-07-12
@mactep3230

ftp = FTP_TLS(ip.rstrip())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question