Reference a directory outside of folder

Question:

How do I reference a file outside of the file’s folder?
I have tried src, but that does not work.

f = open('src/data/beemovie.txt','r')
txt = f.readlines()
print(txt)
f.close()
Asked By: dead inside

||

Answers:

Assume your project structure is like below:

src
└── data
    ├── beemovie.txt
    └── nested
        └── script.py

In order to enable src/data/nested/script.py to read parent directory files, you should use .. to change current working directory to parent directory.

with open('../beemovie.txt', 'r') as f:
    print(f.readlines())
Answered By: user6346643
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.