I'm trying to make a script to back up a MySQL database. I have a config.yml
file:
DB_HOST :'localhost'
DB_USER : 'root'
DB_USER_PASSWORD:'P@$$w0rd'
DB_NAME : 'moodle_data'
BACKUP_PATH : '/var/lib/mysql/moodle_data'
Now I need to read this file. My Python code so far:
import yaml
config = yaml.load(open('config.yml'))
print(config.DB_NAME)
And this is an error that comes up:
file "conf.py", line 4, in <module>
print(config.DB_NAME)
AttributeError: 'str' object has no attribute 'DB_NAME'
Does anyone have an idea where I made a mistake?
yaml.load()
as it can be unsafe and from your question it looks like you don't have the experience to judge correctly if you are affected or not (use.safeload()
instead). Your code also has a problem if readingconfig.yml
throws an error (while parsing), in that case your file might not be closed properly. You should use thewith
statement