Skip to content
← Back to Community
How do you get a file path from repl.it?
Profile icon
GrantKeo

How do you get a file path on repl.it?

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

E.g.:

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 [earned 5 cycles]
View Answer
Voters
Profile icon
Rumble330
Profile icon
Utterubbish
Profile icon
xaiyeon
Profile icon
divyarajparmar
Profile icon
GlowstikkYT
Profile icon
GrantKeo
Comments
hotnewtop
Profile icon
AlexanderBirabe

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

Profile icon
GrantKeo

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

Profile icon
AlexanderBirabe

@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')

https://www.w3schools.com/python/python_file_handling.asp

Profile icon
GrantKeo

@AlexanderBirabe I'll try that then.

Profile icon
GrantKeo

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

Profile icon
AlexanderBirabe

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

import filename

where filename.py is the code you want to run.

Profile icon
GrantKeo

@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 repl.it?

Profile icon
AlexanderBirabe

@GrantKeo Oh ok, you do that like this:

import foldername.filename
Profile icon
GrantKeo

@AlexanderBirabe Ah, thanks!

Profile icon
Party-Hatter

@GrantKeo have you found a way to make it into an input? like import foldername.(variable of whatever file you want to open within the folder)

Profile icon
ZenoxHosting

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

Profile icon
Pecoretta

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

Profile icon
SharpRhyme

@ZenoxHosting Please help

Profile icon
olddft

os.chdir(r'/home/runner/YourReplName/')

Profile icon
ash15khng

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.

Profile icon
GrantKeo

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