I have some functions stored in a separate file, they access data from csv
files. At the beggining of the script I run
current_dir = os.getcwd()
and then all the file names are in the format:
file_name = current_dir + '\\data\\file.csv'
It works well, but only if the function is called from a file that is in the project main directory, otherwise it cant find the \data\
directory
I can set the directory to:
os.chdir('D:\\Projects\\Project\\data')
but I would have to remember to change it manually every time I move it to new machine.
How to set the directory name so that the project is transerable to different computers / platforms and still can run and load the files from director Project/data/
regardless where the Project it on the disk
As far as I understood, you are trying to refer to some files which are in some path in your file system.
I would suggest you to place all of them in your project's root directory and refer to them using relative path as below.
import os
project_root_dir = os.path.dirname(os.path.abspath(__file__))
filepath = project_root_dir + name_of_the_file
#or
filepath = os.path.join(project_root_dir , name_of_the_file)
Or else you need to hardcode it which would cause furtherissues.