Search code examples
pythonpython-3.xadafruit-circuitpython

OSError: [Errno 2] No such file/directory


I am trying to load a font on the Circuitpy using Python and when I run the code I get this error File "code.py", line, 81, in File "/lib/adafruit_bitmap_font/bitmap_font.py", line 44 in load_font OSError: [Errno 2] No such file/directory

The code in that spot is

def load_font(filename, bitmap=None):
    """Loads a font file. Returns None if unsupported."""
    if not bitmap:
        import displayio
        bitmap = displayio.Bitmap
    font_file = open(filename, "rb")    # This is the error spot <<<<<<<<<<<<<<<<<<<<
    first_four = font_file.read(4)
    # print(first_four)
    if filename.endswith("bdf") and first_four == b"STAR":
        from . import bdf
        return bdf.BDF(font_file, bitmap)
    if filename.endswith("pcf") and first_four == b"\x01fcp":
        import pcf
        return pcf.PCF(font_file)
    if filename.endswith("ttf") and first_four == b"\x00\x01\x00\x00":
        import ttf
        return ttf.TTF(font_file)
    return None

All I am trying to do is load the font. We are making an alarm clock in class and I got the code off of the Adafruit website but I am trying to add code to it so it works. I have never coded before so I don't know what I am doing. I am running this code on a pyportal.

What should I do to fix this?

Edit: Ok So I need to define filename, how do I do that so that it will register as the different fonts that I will be using? *


Solution

  • The problem was that I did not have the fonts in the right folder so the pathway was not correct.