从python中的不同目录读取多个csv文件

时间:2020-06-15 18:44:34

标签: python python-3.x python-2.7 csv opencsv

import csv
import pandas

df_list = []
path = "C:/Users/bubai/Desktop/try/scrapy/output"
#all csv file
for file in os.listdir(path):
    #print(file)
    df_list.append(file)  # all csv file in this
#print(df_list) 
for i in df_list:
    df = pandas.read_csv(i)  # open one by one 
    print(df)

我有一些错误:-FileNotFoundError:[Errno 2]文件b'poem1.csv'不存在:b'poem1.csv' 文件名保存为poem1.csv poem10.csv poem11.csv poem12.csv poem13.csv poem14.csv poem15.csv poem16.csv poem17.csv poem18.csv poem19.csv poem2.csv poem20.csv

2 个答案:

答案 0 :(得分:1)

您需要将文件名附加到路径上。

import csv
import pandas
import os

df_list = []
path = "C:/Users/bubai/Desktop/try/scrapy/output"
#all csv file
for file in os.listdir(path):
    df_list.append(os.path.join(path,file))  # all csv file in this
#print(df_list) 
for i in df_list:
    df = pandas.read_csv(i)  # open one by one 
    print(df)

答案 1 :(得分:0)

您需要将目录名与文件名连接起来才能引用该文件。

import os

df = pandas.read_csv(os.path.join(path, i)