r/learnpython • u/hanjokazooie • 2d 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
1
u/hanjokazooie 2d ago
edit: What's a subreddit thats beginner friendly for future questions
1
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 2d ago
This one is at least meant for exactly that.
1
u/Ender_Locke 2d ago
also you can’t have spaces like that. that’s like programming and file directories 101
1
u/hanjokazooie 2d ago
Makes sense, I am 101 right now
1
u/hanjokazooie 2d ago edited 2d 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 2d 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 2d 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
2
u/DHTechdev 2d 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.
5
u/D3str0yTh1ngs 2d ago
I will just start of by telling you that a binary file is not a python file and so using
importon it makes no sense. Do something like this to read in the file:contentsis thrus a variable containing the bytes of the file