Saturday, 28 May 2016

python - Read username and password from a file




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

Answer



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()

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...