尽管正确的相对文件路径,但熊猫仍存在FileNotFoundError

时间:2019-05-23 21:37:34

标签: python mongodb pandas csv pymongo

我正在尝试将同级目录中的csv文件读取到Pandas DataFrame中。我的csv文件的文件路径作为元数据存储在MongoDB集合中,我正在通过pymongo访问该集合。我能够成功提取我的元数据和文件路径,但是当我尝试将csv文件读取到Pandas DataFrame中时,会不断得到一个FileNotFoundError

我的元数据表结构:

{
    "_id" : ObjectId("5ce5df4ab21569c0948ddb74"),
    "DataExtractName" : "DataExtract"
    "SourceDataLocation" : "../sibling_directory/data.csv",
    "DataFrameType" : "pandas"
}

我的元数据提取功能:

def get_meta_data(DataExtractName):
    mongo_client = pymongo.MongoClient('mongodb://localhost:27017')
    df_meta = mongo_client['DBName']['CollectionName'].find_one({'DataExtractName': DataExtractName})
    return df_meta

用于将csv文件读取到pandas DataFrame中的代码:

df_metadata = get_meta_data('DataExtract')
DataFileLocation = df_metadata['SourceDataLocation']
df = pd.read_csv(DataFileLocation)

我得到的错误和部分堆栈跟踪:

df = pd.read_csv(DataFileLocation)
  File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1122, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1853, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 387, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 705, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'../sibling_directory/data.csv' does not exist: b'../sibling_directory/data.csv'

当传入绝对文件路径时,我的程序可以正常工作,但是使用绝对路径会给我带来其他问题,因此这不是我研究的可行选择。

我已参考this问题寻求帮助,并尝试将我的文件路径作为字符串传递给我,但问题仍然存在。

0 个答案:

没有答案