How to open my files in data_folder with pandas using relative path?

前端 未结 11 2486
庸人自扰
庸人自扰 2020-12-22 23:40

I\'m working with pandas and need to read some csv files, the structure is something like this:

folder/folder2/scripts_folder/script.py

folder/fol

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 00:34

    Pandas will start looking from where your current python file is located. Therefore you can move from your current directory to where your data is located with '..' For example:

    pd.read_csv('../../../data_folder/data.csv')
    

    Will go 3 levels up and then into a data_folder (assuming it's there) Or

    pd.read_csv('data_folder/data.csv')
    

    assuming your data_folder is in the same directory as your .py file.

提交回复
热议问题