0
(Lang : Py) Opening file in different path
How do I open file in a different path of my running python script? For ex my python script is in folder A but the file i want to open is in folder B so when I run open(“filename.text”) it won’t open cuz the file is not in the same path as my python script is in so how do i open file from a different folder or different path? Please share me your knowledge.
2 Respuestas
+ 5
The answer may surprise you, but you need to enter the full path of the file you want to open
+ 5
#B/hello.py:
import os
with open(os.path.join('..', 'A', 'hello.txt')) as f:
print(f.read())
#A/hello.txt:
Hello World
#You can use this if files are ordered like this:
#AB
#> A
#>> hello.txt
#> B
#>> hello.py