Ask coding questions

← Back to all posts
Can someone explain how folders, files and main.py work?
OilyOlive (0)

So I just started coding with Python on repl.it and I'm very confused about how the whole folder and file system works, at first I thought that I'd be able to run specific code in a specific file but this doesn't seem to work. Do I just need to make a new repl everytime? And are folder and files purely there for organizational reasons and not to literally seperate code?

Comments
hotnewtop
SixBeeps (5218)

The folder structure on Replit works exactly as it does on a regular computer. There are a variety of reasons why you might want to create different files and folders, especially if you're creating a module. Another use would be to separate different sub-projects that you may have going on under one large project. If you ever decide to get into web development, this would be good for separating server code and client code.

By default, Repls are set up to always run whatever's in main.py. If you want to run code in a different file, you'll have to invoke the python command yourself in the Bash console. You can also use the .replit file to make the Run button run different files.

OilyOlive (0)

@SixBeeps Thank you for the advice, I think that cleared up a lot of things. However, I didn't understand your advice on making the run button run specific files. Could you possibly explain this in simpler terms?

SixBeeps (5218)

@OilyOlive Sure. Are you familiar with running Python scripts through the command prompt or terminal?

SixBeeps (5218)

@OilyOlive Okay then, I shall explain.

Under normal circumstances, to run a Python script named CoolScript.py in the current directory, you'd run the following command in your console:

python CoolScript.py

Replit automatically calls python main.py for Python Repls when you hit the run button. However, with the .replit file, you can change this. Say you wanted to run the same script instead of main.py. To do that, you'd put this in the .replit file:

RUN="python CoolScript.py"

If the script is in a different folder, however, we need to specify that the script is in that folder. If CoolScript.py is in a folder called AwesomeFolder, put this in instead:

RUN="python ./AwesomeFolder/CoolScript.py"
OilyOlive (0)

@SixBeeps Thank you so much for this. It worked perfectly and now I can organize my code even better. I really appreciate it.