0
I have the following situation:
Within the
Pasta1\i have a file that is my main codeThis file
codigo_principal.pyreads a file that is in a subfolder within thePasta1\I also have another folder,
Pasta2, and inside it I have a code,teste.pyThe archive
teste.pyneed to use file functioncodigo_principal.pyreadingSub_pasta1\arquivo.txt
Follows the folder structure:
Pasta1/
Sub_pasta1/
arquivo.txt
codigo_principal.py
Pasta2/
teste.py
The problem has already arisen when I needed to import this file from a different folder, I managed to get around this problem using the module imp
I can use all the resources of codigo_principal.py, but the function that reads the arquivo.txt does not work because it tries to fetch this file inside the Pasta2\SubPasta1\arquivo.txt.
EDIT: I am using and need to use the relative path, I cannot use the absolute
This is the code I’m using on 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_pasta1/file.txt

Are you using the absolute path to open the file? If it is relative, the path is relative to the directory
Pasta2, looking forPasta2/Sub_pasta1/arquivo.txt.– Woss
I forgot to quote, I need to use the relative path
– José Henrique Luckmann
Then enter the path as a function parameter and in the file
teste.pyyou set the path relative toPasta2.– Woss