Ask coding questions

← Back to all posts
How do you get a file path from repl.it?
GrantKeo (86)

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 (101) [earned 5 cycles]
View Answer
Comments
hotnewtop
AlexanderBirabe (101)

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

GrantKeo (86)

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

AlexanderBirabe (101)

@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

GrantKeo (86)

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

AlexanderBirabe (101)

@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.

GrantKeo (86)

@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?

AlexanderBirabe (101)

@GrantKeo Oh ok, you do that like this:

import foldername.filename
ZenoxHosting (12)

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 (712)

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 (86)

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