Ask coding questions

← Back to all posts
How do you get a file path from
GrantKeo (87)

How do you get a file path on

I'm guessing that there's some trick I don't know when getting a file path through I want to use folders to organize my files, such as the path for a txt file.


import os, sys

sys.path.insert(0, 'C:\\some\\path\\goes\\here\\to\\this\\folder')

So what am I missing? I'm guessing it has something to do with the little "(home)/" at the topleft corner of "my repls," but what is it exactly? (And by the way, can you please provide the answer in Python 3? I use that language for everything.)

Thanks in advance!

Answered by AlexanderBirabe (102) [earned 5 cycles]
View Answer
AlexanderBirabe (102)

You can only access files in the same repl unless you make an API or something like that.

GrantKeo (87)

@AlexanderBirabe Well, how do I do that in that case?

AlexanderBirabe (102)

@GrantKeo In python you would just do:

myFile = open('filename.txt', 'r')

To open the file 'filename.txt' in read mode (as long as it's in the same folder.

If say filename.txt was in a folder called 'myFolder' and myFolder was in the same folder as the script you are using to reference it, it would be:

myFile = open('myFolder/filename.txt', 'r')

GrantKeo (87)

@AlexanderBirabe But how do you upload a .py file from the same project?

AlexanderBirabe (102)

@GrantKeo By upload to you mean run? If so, you do it like this:

import filename

where is the code you want to run.

GrantKeo (87)

@AlexanderBirabe However, import filename does not work when the py file is inside a folder. I'm asking how do you import a py file from another folder using

AlexanderBirabe (102)

@GrantKeo Oh ok, you do that like this:

import foldername.filename
ZenoxHosting (14)

Yeah I was wondering if there was a way to access files inside a repl project. I am doing os.chdir('mainbank.json') and it says that it is not a directory. I am pretty new to coding with Repl so it would be nice if anyone could help me out. Thanks :D

Pecoretta (0)

have you solved your issue? if yes, how? @ZenoxHosting

ash15khng (721)

The (home)/ thing is just a way for you to find your repls and I'm quite sure it's just for you to organise your repls which means you can't access stuff from other repls.

GrantKeo (87)

@ash15khng I had not actually wanted to access from other files, but I want to know the file path inside a project.