7

I have a file .env file contain 5 lines

DB_HOST=http://localhost/
DB_DATABASE=bheng-local
DB_USERNAME=root
DB_PASSWORD=1234567890
UNIX_SOCKET=/tmp/mysql.sock

I want to write python to grab the value of DB_DATABASE I want this bheng-local


I would have use

import linecache
print linecache.getline('.env', 2)

But some people might change the order of the cofigs, that's why linecache is not my option.


I am not sure how to check for only some strings match but all the entire line, and grab the value after the =.

I'm kind of stuck here :

file = open('.env', "r")
read = file.read()
my_line = ""

for line in read.splitlines():
    if line == "DB_DATABASE=":
        my_line = line
        break
print my_line

Can someone please give me a little push here ?

mtt2p
  • 1,818
  • 1
  • 15
  • 22
code-8
  • 54,650
  • 106
  • 352
  • 604

5 Answers5

10

Have a look at the config parser: https://docs.python.org/3/library/configparser.html

It's more elegant than a self-made solution

yar
  • 1,855
  • 13
  • 26
  • I like the idea of using the parser, but it require me to add more texts on top of my configs file. I don't mind if I am the only one that has it. This config file s is deployed to thousand people already. :( Is there a simple way to grab it base on DB_DATABASE which is the string of file ? – code-8 Mar 03 '17 at 00:48
  • There are some hacks to go around it, you can find them [here](http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/): The shortest is probably [this one](http://stackoverflow.com/a/25493615/3022712) , for Python 3.2+ and [this one](http://stackoverflow.com/a/8657601/3022712) for earlier versions. The most elegant probably [this one](http://stackoverflow.com/a/2819788/3022712). – yar Mar 03 '17 at 03:14
9

Modify your .env to

[DB]
DB_HOST=http://localhost/
DB_DATABASE=bheng-local
DB_USERNAME=root
DB_PASSWORD=1234567890
UNIX_SOCKET=/tmp/mysql.sock

Python code

#!/usr/local/bin/python
import configparser
config = configparser.ConfigParser()
config.read('test.env')
print config.get('DB','DB_DATABASE')

Output:

bheng-local

Read https://docs.python.org/3/library/configparser.html

Knase
  • 1,224
  • 14
  • 23
Sridhar Iyer
  • 189
  • 1
  • 7
  • I like the idea of using the parser, but it require me to add more texts on top of my configs file. I don't mind if I am the only one that has it. This config file s is deployed to thousand people already. :( Is there a simple way to grab it base on DB_DATABASE which is the string of file ? – code-8 Mar 03 '17 at 00:48
4

This should work for you

#!/usr/local/bin/python
file = open('test.env', "r")
read = file.read()

for line in read.splitlines():
    if 'DB_DATABASE=' in line:
        print line.split('=',1)[1]
Sridhar Iyer
  • 189
  • 1
  • 7
2
#!/usr/local/bin/python

from configobj import ConfigObj
conf = ConfigObj('test.env')
print conf['DB_DATABASE']
subh8899
  • 21
  • 3
  • 1
    Using ConfigObj, we do not need to add anything like section header at the start of the .env file to read the variable values from it. – subh8899 Jan 11 '19 at 15:03
  • And without a header, the configuration file can also be read by other languages easily, like Bash for example. – Hans Deragon Jan 27 '21 at 19:50
1
from os import environ, path
from dotenv import load_dotenv

basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, '.env'))
DB_DATABASE = environ.get('DB_DATABASE')
print(DB_DATABASE)

This could be another option

lejencodes
  • 190
  • 7