Created
December 27, 2022 17:52
-
-
Save zach2825/f7c460dc535d6fe45cf338f29e120d36 to your computer and use it in GitHub Desktop.
python read venv value or all keys into a dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict, Union | |
def read_env(key: str = False, default_value: str = '') -> Union[str, Dict[str, str]]: | |
""" | |
Open the venv/pyvenv.cfg file for reading and return a key value or all the settings | |
:param key: | |
:param default_value: | |
:return: | |
""" | |
with open("venv/pyvenv.cfg", "r") as f: | |
lines = f.readlines() | |
pairs = {key: value for key, value in (line.split(" = ") for line in lines)} | |
if key is not False: | |
return pairs.get(key, default_value) | |
return pairs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage