Read all files in project folder with variable path – Python

  glob, operating-system, python

Looking for thoughts on how to read all csv files inside a folder in a project.

As an example, the following code is a part of my present working code, where my ‘ProjectFolder’ is on Desktop, and I am hardcoding the path. Inside the project folder, I have ‘csvfolder’ where I have all my csv files

However if I move the "ProjectFolder" to a different Hard drive, or other location, my path fails and I have to provide a new path. Is there an smart way to not worry about location of the project folder?

path = r'C:UsersXXXDesktopProjectFoldercsvFolder' # use your path
all_files = glob.glob(path + "/*.csv")

df_mm = pd.concat((pd.read_csv(f, usecols=["[mm]"]) for f in all_files),
               axis = 1, ignore_index = True)

Source: Python Questions

LEAVE A COMMENT