jupyter-notebookoserror

ERROR: OS Error in Jupyter Notebook indicating that the file path argument provided to the open() function is invalid


I've tried to open a file from my system using its file path ("E:\Project\End-to-End Cricket Data Analytics\Resources\data-analytics-project-for-beginners\t20_json_files\t20_json_files\t20_wc_match_results.json") by using open() in the Jupyter Notebook.

It throwed the following OS Error:

OSError                                   Traceback (most recent call last)
Cell In[4], line 1
----> 1 with open("E:\Project\End-to-End Cricket Data Analytics\Resources\data-analytics-project-for-beginners\t20_json_files\t20_json_files\t20_wc_match_results.json") as f:
      2     data=json.load(f)
      3 data

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\core\interactiveshell.py:284, in _modified_open(file, *args, **kwargs)
    277 if file in {0, 1, 2}:
    278     raise ValueError(
    279         f"IPython won't let you open fd={file} by default "
    280         "as it is likely to crash IPython. If you know what you are doing, "
    281         "you can use builtins' open."
    282     )
--> 284 return io_open(file, *args, **kwargs)

OSError: [Errno 22] Invalid argument: 'E:\\Project\\End-to-End Cricket Data Analytics\\Resources\\data-analytics-project-for-beginners\t20_json_files\t20_json_files\t20_wc_match_results.json'

Tried moving the file to different path where the folder name and file name are not similar. Also tried by giving only the name of the file excluding its file path still the issue isn't solved.


Solution

  • The OS Error encountered is due to an invalid argument in the file path. It seems there are some issues with the backslashes () and escape characters in the file path string.

    Escape backslashes: Escape the backslashes in the file path by adding an extra backslash before each backslash.

    with open("E:\\Project\\End-to-End Cricket Data Analytics\\Resources\\data-analytics-project-for-beginners\\t20_json_files\\t20_json_files\\t20_wc_match_results.json") as f:
        data = json.load(f)