我有一个带有et
文件的文件夹.csv
,我尝试阅读该文件,然后连接并获取一个文件。
我试试
import os
path = 'et/'
for filename in os.listdir(path):
et = open(filename)
print et
但是我收到了错误
Traceback (most recent call last):
File "C:/Users/����� �����������/Desktop/projects/PMI/join et.py", line 5, in <module>
et = open(filename)
IOError: [Errno 2] No such file or directory: '0et.csv'
我无法理解,为什么我会得到这个错误,因为当我
print filename
我得到了
0et.csv
1et.csv
2et.csv
3et.csv
4et.csv
5et.csv
6et.csv
7et.csv
8et.csv
答案 0 :(得分:0)
您可能希望使用et = open(path+filename)
,而不只是et = open(filename)
。
编辑:根据@thiruvenkadam的建议,最佳做法是使用et = open(os.path.join(path,filename))
答案 1 :(得分:0)
使用glob.glob将是一个更好的选择,同时使用os.path.join来获取完整路径:
from glob import glob
from os.path import join, abspath
from os import listdir, getcwd
import pandas as pd
data_frame = pd.DataFrame()
dir_path = "et"
full_path = join(abspath(getcwd()), dir_path, "*.csv")
for file_name in glob(full_path):
csv_reader = pd.read_csv(file_name, names=columns)
# Guessing that all csv files will have the header
#If header is absent, use names=None
data_frame = data_frame.append(csv_reader, ignore_index=True)
# There is also a concat funtion to use. I am comfortable with append
# For concat, it will be data_frame = pd.concat(data_frame, csv_reader, ignore_index=True)
答案 2 :(得分:-1)
也许是编码问题
您可以尝试在代码顶部添加以下代码
# -*- coding: utf-8 -*-