Open file from another location by relative path


I need to open a file that is in another directory that is being worked on, I tried with:


but when I run the script I get:


FileNotFoundError: [Errno 2] No such file or directory: 'folder / subfolder1 / subfolder2 / file.log'

The complete code is:


import sendMail
from sendgrid.helpers.mail import *
from subprocess import call
import sys
import subprocess
import os.path
import unittest
from importlib import util

result = open("../ISV1/testing/log/testResult.log")
print (result)

The structure of my project is as follows:

├── ISV1
│ ├── testing (acá tengo los scripts de los test)
│     ├── log 
│          └── (archivo que quiero leer)
│── server-test
│ └── 

What am I doing wrong? I put the inverted bars as they recommended me, and nothing.

asked by Yamila Marucci 30.01.2018 в 05:27

2 answers


First of all, you must use the diagonal bar as a separator. UNIX systems use a forward slash ( / ) unlike Windows that uses a backslash ( \ ) as a route separator. Otherwise, it is taken as escape sequences, which causes, for example, in \log\testResult.log \t to be a tabulation.

On the other hand, starting from the following structure (which is what you seem to have your project) and assuming that your main script (the one you execute) is (located within test-server ):

You must position yourself in the parent directory that contains both ISV1 and test-server . To do this simply use the syntax (dot dot):

result = open("../ISV1/testing/log/testResult.log")
answered by 31.01.2018 / 09:50

I have tried to open a file just as you comment. The first thing you should do is assign the open file to a variable:

archivo = open("carpeta/subcarpeta1/subcarpeta2/archivo.log")

And try changing the \ to / in the directory.

This should solve your problem.

answered by 30.01.2018 в 08:40