5

How can i read text, e.g., username and password from a file line-by-line in Python? For example, i can achieve this in shell/bash:

#!/bin/bash

AUTH='/authentication/account.txt'
U=$(sed -n 1p ${AUTH})
P=$(sed -n 2p ${AUTH})

and inside the file /authentication/account.txt are my username and password line-by-line like this:

username
userpass
1
  • Have you tried to research this? There are a million and one sources + questions on how to do this. Commented Aug 3, 2017 at 0:42

1 Answer 1

15

You should not be storing unencrypted login details in text files.

However, here is a very simple solution anyway:

f=open("/authentication/account.txt","r")
lines=f.readlines()
username=lines[0]
password=lines[1]
f.close()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.