-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.py
57 lines (40 loc) · 1.34 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import os
from dotenv import load_dotenv
def get_env_variable(key):
load_dotenv()
value = os.getenv(key)
# If the variable is not found, ask the user to input it
if value is None:
value = input(f"Enter the value for {key}: ")
# Optionally, you can also save this back to the .env file
save_to_env(key, value)
return value
def extract_id(song_dict):
ids = [str(item[key]) for item in data_dict.values() if key in item]
# Write IDs to a file
with open("songid.txt", 'w') as file:
for id in ids:
file.write(id + '\n')
def load_json(file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
return data
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
except json.JSONDecodeError:
print(f"Error decoding JSON from file: {file_path}")
return None
def save_to_env(key, value):
with open('.env', 'a') as env_file:
env_file.write(f"{key}={value}\n")
json_file_path = 'output.json'
loaded_data = load_json(json_file_path)
if loaded_data is not None:
print("Loaded JSON content:")
print(json.dumps(loaded_data, indent=2))
key_name = 'MY_ENV_VARIABLE'
env_value = get_env_variable(key_name)
print(f"The value of {key_name} is: {env_value}")