How to read an .ipynb file type in a python code

163 Views Asked by At

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.

1

There are 1 best solutions below

0
C.Nivs On

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