Open file from another location by relative path

2

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

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

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:

!/usr/local/bin/python3.5

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

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

The structure of my project is as follows:

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

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
source

2 answers

1

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 script.py (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
source
0

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