[Python] Error : Please provide the path to the JSON file as an argument [closed]

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    The program takes as its first argument the path to a file containing the data in JSON format containing the data to be processed.

    But I get an error with my file when I run the script using the command python3 schools_out.py to terminal

    ***

    import json
    import sys

    from typing import List, Dict, Any, Tuple

    class disponibility_class:

    def __init__(self):
    self.sessions = []

    def load_sessions(self, file_path: str) -> List[Dict[str, Any]]:


    Returns:
    List[Dict[str, Any]]: Liste des objets de session
    """
    try:
    with open(file_path, 'r', encoding='utf-8') as file:
    self.sessions = json.load(file)
    return self.sessions
    except FileNotFoundError:
    print(f"Error: File {file_path} not found.")
    return []
    except json.JSONDecodeError:
    print(f"Error: Invalid JSON format in {file_path}")
    return []

    # Example usage
    if __name__ == "__main__":
    if len(sys.argv) < 2:
    print("Please provide the path to the JSON file as an argument.")
    sys.exit(1)

    scheduler = disponibility_class()
    sessions = scheduler.load_sessions(sys.argv[1])

    # Print all sessions
    for session in sessions:
    print(f"Session: {session['session_name']}")
    print(f"Teacher: {session['teacher_name']}")
    print(f"Time: {session['start']} - {session['end']}")
    print("-" * 40)

    Continue reading...

Compartilhe esta Página