Nima Azhdari
Nima Azhdari

Reputation: 55

python new line in txt file

I have written some code to save usernames, passwords and email addresses in a text file. by reading an email from an input text file and writing it with the other information to an output text file.

However, in the output file, new-lines are appearing after the email.

For example:

[email protected]
,name,user,pass

After changing the order of "email" and "name" it is happening again:

name,[email protected]
,user,pass.

My email file looks like:

[email protected]
[email protected]
...

How do I fix this issue? Thanks in advance

My code:

line=""
driver = webdriver.Firefox()
f=open(r'C:\Users\11\Desktop\emailes.txt','r')

while True:
    password=id_generator()
    username=checkname()
    email=f.readline()
    if username !="":
            f2=open(r'C:\Users\11\Desktop\info.txt','a')
            f2.write(  email + "," + username + "," + username + "," + 
            password)
            f2.write("\n")
            driver.get("xxxx")
            assert "xxxx" in driver.title
            driver.implicitly_wait(5)
            elem=driver.find_element_by_name("email")
            elem.clear
            elem.send_keys(email)
            elem=driver.find_element_by_name("fullName")
            elem.clear
            elem.send_keys(username)
            elem=driver.find_element_by_name("username")
            elem.clear
            elem.send_keys(username)
            elem=driver.find_element_by_name("password")
            elem.clear
            elem.send_keys(password)
            elem.send_keys(Keys.RETURN)
            f2.close()
f.close()

Upvotes: 1

Views: 1721

Answers (2)

nacho
nacho

Reputation: 5396

When you read the email, it gets an EOF ('\n') character because you are reading a line. You need to do this

email=f.readline().replace('\n','')

so you take off the EOF

Upvotes: 0

Dan Nagle
Dan Nagle

Reputation: 5425

The issue is the string returned by the readline function will include the \n character.

Change:

email=f.readline()

to

email=f.readline().strip()

Upvotes: 1

Related Questions