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
martineau
  • 119,623
  • 25
  • 170
  • 301
The Georgia
  • 1,005
  • 7
  • 23
  • 59
  • Have you _tried_ to research this? There are a million and one sources + questions on how to do this. – cs95 Aug 03 '17 at 00:42

1 Answers1

13

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()
Syd Lambert
  • 1,415
  • 14
  • 16