Eugene Nosenko Eugene Nosenko - 2 years ago 88
Python Question

Variables from txt file

I have a txt file that stores user IDs for

tests that has something like this:

admin = ['', 'password1234']
basicUser = ['', 'password1234']

and would like to assign values for
from my txt file to my Python script, say something like this:

import file.txt
file = open('file.txt', 'r')
admin = file(admin)
basicUser = file(basicUser)

and whenever I call the
method for my LoginPage


it would automatically update and save the txt file for next login.

I've been browsing the forums for hours now and cant find any good/elegant solution, any idea?

Answer Source

You can use .yaml file to store your users. It stores your date like json fotmat.

admin: {passwd: 123, user:}
basicUser: {passwd: pass45, user:}

To load your data from file:

import yaml
with open(FILE_PATH, 'r') as f:
   users_dict = yaml.load(f)
### {passwd: 123, user:}
### 123

After loading you can work with the data like a dict

To save your users data:

users_dict['admin']['passwd'] = 345

with open(FILE_PATH, 'w') as f:
   yaml.dump(users_dict, f)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download