(Lang : Py) Opening file in different path | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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.

27th Oct 2018, 2:43 AM
Tan Vireak Punhakvorn
Tan Vireak Punhakvorn - avatar
2 Antworten
+ 5
The answer may surprise you, but you need to enter the full path of the file you want to open
27th Oct 2018, 5:18 AM
Anna
Anna - avatar
+ 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
27th Oct 2018, 9:41 AM
Mert Yazıcı
Mert Yazıcı - avatar