I have the following situation:
-
Within
Pasta1\
I have a file that is my main code -
This file
codigo_principal.py
reads a file that is in a subfolder withinPasta1\
-
I also have another folder,
Pasta2
, and inside it I have a code,teste.py
-
The file
teste.py
needs to use the function of the filecodigo_principal.py
that readsSub_pasta1\arquivo.txt
Follow the structure of the folders:
Pasta1/
Sub_pasta1/
arquivo.txt
codigo_principal.py
Pasta2/
teste.py
The problem already arose when I needed to import this file from a different folder, I was able to work around this problem using the module imp
I can use all the features of codigo_principal.py
, but the function that reads arquivo.txt
does not work because it tries to fetch this file from Pasta2\SubPasta1\arquivo.txt
.
EDIT: I'm using and need to use the relative path, I can not use the absolute
This is the code I'm using in teste.py
import imp
wtf=imp.load_source("codigo_principal", "../Pasta1/codigo_principal.py")
wtf.funcao_le_arquivo()
But this way I get the error message:
FileNotFoundError: [Errno 2] No such file or directory: Sub_folder1 / file.txt