Reputation: 17
So if I open a folder in vs code that stores the files or modules I need to open or import, my code works fine, however if I open a folder higher in the directory, I get errors such as file not found.
Path: C:\Users\user1\Desktop\Programming\Python\30DaysPython\Day10
My .py file is in Day10, if I open VS code in that folder my program works, but if I open VS in 30DayPython, it doesn't work.
Why do I have to open vs in the folder that I am working on (containing my files), shouldn't it use the relative path of the file I am running and not where I open vs code from?
My code:
import os
email_txt = os.path.join('templates', 'email.txt')
content = ''
with open(email_txt, 'r') as f:
enter code herecontent = f.read()
print(content.format(name='Joe'))
Image of opening vs code in 1 folder higher than the folder that contains the program and it doesn't work.
Upvotes: 0
Views: 361
Reputation: 9451
That's because your current working directory changed. When you open 30DaysPython as current folder, you have to change the path code to
email_txt = os.path.join('Day10\\templates', 'email.txt')
Then the error should go away.
Upvotes: 1