This is a Python JSON Generator created using Python File Handling, Python Exceptions and Python Modules.
-
Python 3.11 and above — Follow this link to download Python for your operating system. You can refer to these tutorials on YouTube for the same — Windows, Mac-OS, Ubuntu (it’ll be somewhat similar for other Linux distributions).
-
pip — pip has been included with the Python installer since versions 3.4 for Python 3 and 2.7.
-
Install pipenv using pip — pip install pipenv
- Install required dependencies using pipenv — pipenv install
- Activate Python Virtual Environment — pipenv shell
- Run the program — python main.py
- Github: Code
-
Python Exceptions
-
Try/ Except Statements
-
Python File Handling
-
Creating and Reading Files in Python
-
Accessing Modules
-
Writing Import Statements
Here is a code snippet:
import json
from input_module import get_employee_details
def read_json_file(file_path):
try:
with open(file_path, 'r') as f:
employee_list = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
employee_list = []
return employee_list
def write_json_to_file(json_obj, output_file):
with open(output_file, 'w') as f:
json.dump(json_obj, f, indent=4)
def main():
file_path = './employees.json'
employee_list = read_json_file(file_path)
while True:
employee_dict = get_employee_details()
employee_list.append(employee_dict)
choice = input(
"Do you want to enter details for another employee? (Y/N): ")
if choice.lower() == 'n':
break
write_json_to_file(employee_list, file_path)
if __name__ == '__main__':
main()
- Python File Handling - This helped me understand the concept of File Handling in Python.
- Python Exceptions - This helped me understand the use Python Exceptions.
- Pipenv Setup - This helped me setup my python virtual env
- Website - Marvin Morales Pacis
- LinkedIn - @marventures
- Twitter - @marventures11