Skip to content
Advertisement

Is it possible to read a csv file that is located in the python project?

I have a python project with a handful of python scripts, a domain and repository folder with additional python scripts in each etc…. I’d like to create a config folder within this python project that will contain a csv file for configurations that my python project will use.

I then would like to read this csv file into a dataframe within my python code. Is this possible? Any search I’ve done only details reading csv files from an actual file share location like from a C drive or desktop location.

df_config = pd.read_csv('Python Config Folder/File_Config.csv', delimiter = ",")

Advertisement

Answer

You can use the current directory for this.
Example:

import pathlib
actual_dir = pathlib.Path().absolute()
df_config = pd.read_csv(f'{actual_dir}/File_Config.csv', delimiter = ",")

If you want get the previus directory you can use “..”
Exemple:

f'{actual_dir}/../File_Config.csv'
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement