Skip to content
← Back to Community
Python, Point to folder and execute custom.py from main.py
Profile icon
Pyron

So, I can't delete main.py and that is okay.

However, I am practicing a bunch of code in a repl and I would love to seperate each practice script in it's own folder, but I am having trouble with opening a path and then executing my script from main.py (since everything runs from main.py.

Here is what I am working with:
#--------------------------------------
from pathlib import Path

dataFolder = Path("/HelloWorld")
fileToOpen = dataFolder / "helloworld.py"

openFile = open(fileToOpen)

print(openFile.read())

Answered by Steven_The_GuyT [earned 5 cycles]
View Answer
Voters
Profile icon
preethamam
Profile icon
MarcelinoDe1
Profile icon
SonyWong
Profile icon
Lord_Poseidon
Profile icon
Pyron
Comments
hotnewtop
Profile icon
Steven_The_GuyT

You don't have to use different folders to run different code, you can just assign functions to your code. Call your function when you need to run it, so you only need main.py.

Profile icon
Lord_Poseidon

Press ctrl+shift+p. In the menu, choose open shell. This will open a shell where you can cd into your files and just run them as you would offline, python my.py.