我在将csv文件写入MongoDB时遇到问题

时间:2019-06-22 08:58:56

标签: python filepath

我正在尝试将一个文件夹中的所有csv文件写入MongoDB集合。但是当目录中存在文件时,由于找不到文件,我不断收到错误消息。

output_files = [file for file in os.listdir(dir_path) if file.endswith(".csv")]
print("Files in the folder:", output_files)

#create mongoclient object    
client = MongoClient()

#get the database and list the collections
db = client.senci
collection_names_list = db.list_collection_names()
print("MongoDB collections:", collection_names_list)

for file in output_files:
    collection = "senci_" + file[:-4]

    if collection in collection_names_list:
        #print("File exists. Documents in this collection will be deleted.")
        col = db[collection].delete_many({})
        print(col.deleted_count, " documents deleted from", collection)

        #write new data
        df = pd.read_csv(file, engine='python', delimiter=',')
        records_ = df.to_dict(orient = 'records')
        db[collection].insert_many(records_)
        print("Collection updated.\n")

以下是我得到的错误。前三行用于确保csv存在。

Files in the folder: ['groceries.csv']
MongoDB collections: ['senci_adult_diapers', 'senci_health_supplements', 'senci_groceries', 'senci_mobility_aids']
0  documents deleted from senci_groceries
Traceback (most recent call last):
  File "tt.py", line 27, in <module>
    df = pd.read_csv(file, engine='python', delimiter=',')
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 1132, in _make_engine
    self._engine = klass(self.f, **self.options)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 2225, in __init__
    memory_map=self.memory_map)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\common.py", line 427, in _get_handle
    f = open(path_or_buf, mode, errors='replace', newline="")
FileNotFoundError: [Errno 2] No such file or directory: 'groceries.csv'

1 个答案:

答案 0 :(得分:1)

您要传递文件的名称,您需要传递完整的路径os.path.join函数可用于组合目录路径和文件名

import os.path

# Gets *names*
output_files = [file for file in os.listdir(dir_path) if file.endswith(".csv")]

...    

for file in output_files:

    # Get full path
    path = os.path.join(dirpath, file)

    # write new data
    df = pd.read_csv(path, engine='python', delimiter=',')