0

I'm trying to get my program to read an .ipynb file type that contains graphs, images and text:

f = open(r"~/Documents/Dev/Python/facial-emotion-recognition.ipynb","r")
print(f.read())

but it keeps printing

FileNotFoundError: [Errno 2] No such file or directory

I already tried adding the file directory but that still doesn't work. I'm doing this on a chrome book where this file type isn't supported but I installed jupyter and could open the file on that.

indigoBLU
  • 1
  • 1
  • SeeS https://stackoverflow.com/questions/2057045/os-makedirs-doesnt-understand-in-my-path – slothrop Aug 08 '23 at 14:20
  • Once you solve your path issue, you'd be better of using `nbformat` to read in an `.ipynb` file. It has the concepts of cell types and output cells and things built in and allows you to more easily parse an `.ipynb` file. See [here](https://stackoverflow.com/a/74379308/8508004) and [here](https://discourse.jupyter.org/t/parsing-the-output-of-a-jupyter-notebook/12524/2?u=fomightez) and links therein for more about nbformat and examples with code. Essentially after import, you can run `ntbk = nbf.read("/facial-emotion-recognition.ipynb", nbf.NO_CONVERT)` and then step thru it. – Wayne Aug 08 '23 at 14:32

1 Answers1

0

Python doesn't know what ~ is. You can use either os.path.expanduser:

import os

path = os.path.join(
    os.path.expanduser('~'), 
    'Documents/Dev/Python/facial-emotion-recognition.ipynb'
) 

with open(path) as fh:
    # do something

Or you can use pathlib.Path.home():

from pathlib import Path

path = (
    Path.home() / 
    'Documents/Dev/Python/facial-emotion-recognition.ipynb'
)

with open(path) as fh:
    # do something
C.Nivs
  • 12,353
  • 2
  • 19
  • 44