I turned my Python script into an executable. How can the program identify which directory it is in?
Question:
I have an executable that exports a CSV. I want the CSV to be exported to the same folder where the executable is kept.
Is there something I can put in the script so that, if the executable is moved to a different folder, the CSV will be saved in that new folder?
Answers:
To be on the save side you can make use of the pathlib module.
from pathlib import Path
path_script = Path(__file__)
path_csv = path_script.parent.joinpath('my_table.csv')
with open(path_csv, 'w') as csv:
...
I have an executable that exports a CSV. I want the CSV to be exported to the same folder where the executable is kept.
Is there something I can put in the script so that, if the executable is moved to a different folder, the CSV will be saved in that new folder?
To be on the save side you can make use of the pathlib module.
from pathlib import Path
path_script = Path(__file__)
path_csv = path_script.parent.joinpath('my_table.csv')
with open(path_csv, 'w') as csv:
...