Home > Back-end >  Reference a directory outside of folder
Reference a directory outside of folder

Time:08-12

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()

CodePudding user response:

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())
  • Related