In Python, I'm trying to access a file from the directory where the "last" function (for lack of a better word) is located.
For example, let's suppose I have the following files:
C:/
foo.py
package/
helper.py
text.md
foo.py has:
from package import helper
helper.edit_file("text.md")
helper.py has:
from os.path import abspath
def edit_file(file_location):
with open(abspath(file_location), "w") as file:
file.write("This file has been written to.")
And text.md is just an empty text file.
When I directly run edit_file("text.md")
from helper.py, it writes to package/text.md properly. However, when I run foo.py, it doesn't write to package/text.md and instead creates a new text.md in the same directory level as foo.py. I'm assuming that this happens because it's taking an absolute path from where foo.py is located.
Is it possible to only take the absolute path from helper.py, so that it always writes to package/text.md, no matter where the original call to edit_file(file_location)
is?