r/learnpython • • 4d ago

Trying to import a file into Thonny

Trying to import a rom file to convert to bytes and then mod, but the game title has "of" in it, so it returns as a syntax error. I guess the IDE thinks I'm trying to write an if statement? Anyway, google wasn't helpful, so i came here.
Code below
import Breath of Fire II (USA) - Copy.sfc

2 Upvotes

14 comments sorted by

5

u/D3str0yTh1ngs 4d ago

I will just start of by telling you that a binary file is not a python file and so using import on it makes no sense. Do something like this to read in the file:

with open("Breath of Fire II (USA) - Copy.sfc", "rb") as f:
    contents = f.read()

contents is thrus a variable containing the bytes of the file

1

u/hanjokazooie 4d ago

thank you!

2

u/quietcov3 4d ago

mixing up importing modules vs opening normal files is such a classic beginner trap tbh

1

u/hanjokazooie 4d ago

edit: What's a subreddit thats beginner friendly for future questions

1

u/ninhaomah 4d ago

This isn't ?

1

u/hanjokazooie 2d ago

Ehhh it seems like its meant for those 1/2 step up from absolute beginner

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 4d ago

This one is at least meant for exactly that.

1

u/Ender_Locke 4d ago

also you can’t have spaces like that. that’s like programming and file directories 101

1

u/hanjokazooie 4d ago

Makes sense, I am 101 right now

1

u/hanjokazooie 4d ago edited 4d ago

Although this one part doesn't make sense to me, because its not a variable name. Why does import need to be "importBreathofFireII" as compared to something like "if XYZ is ABC" /genquestion

2

u/Slothemo 4d ago

Again, importing is only for bringing in modules/libraries specific to Python. They are saying you should never name a file or folder with a space. Stick to letters/numbers/underscore only.

2

u/socal_nerdtastic 4d ago

It's a good question. The python developers could have chosen to make

import "module name with spaces"

possible. But they didn't. Python requires that all imported modules follow the same name rules that python variables have, so no spaces, no special characters, no starting with a digit, etc. That's just one of the rules of the language.

2

u/argh1989 4d ago

Import is for modules, not files.

2

u/DHTechdev 4d ago

The short version is that import wants a module name, not a file path. Python reads "import Breath of Fire II" as "import Breath" followed by stray words, so it chokes at "of". "if XYZ is ABC" works because if has its own grammar that accepts any expression after it. For the rom, open it with open(path, "rb") and read the bytes like the earlier reply showed.