Skip to content

This is a Python JSON Generator created using Python File Handling, Python Exceptions and Python Modules.

Notifications You must be signed in to change notification settings

marventures/python-json-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python JSON Generator

This is a Python JSON Generator created using Python File Handling, Python Exceptions and Python Modules.

Table of contents

Overview

How to use the project

Step 1: Setting up Initial Requirements:

  1. 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).

  2. pip — pip has been included with the Python installer since versions 3.4 for Python 3 and 2.7.

  3. Install pipenv using pip — pip install pipenv

Step 2: Setting up Python Virtual Environment using pipenv:

  1. Install required dependencies using pipenv — pipenv install
  2. Activate Python Virtual Environment — pipenv shell
  3. Run the program — python main.py

Screenshot

image image

Links

My process

Built with

What I learned

  • 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()

Useful resources

Author

About

This is a Python JSON Generator created using Python File Handling, Python Exceptions and Python Modules.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages