I have a folder et
with .csv
files and I try to read that and next concatenate that and get one file. I try
import os
path = 'et/'
for filename in os.listdir(path):
et = open(filename)
print et
but I get an error
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'
I can't understand, why I get this error, because when I print filename
I get
0et.csv
1et.csv
2et.csv
3et.csv
4et.csv
5et.csv
6et.csv
7et.csv
8et.csv
You probably want to use et = open(path+filename)
, instead of just et = open(filename)
.
Edit : as suggested by @thiruvenkadam best practice would be to use et = open(os.path.join(path,filename))
Using glob.glob will be a better option, along with using os.path.join to get to the full path:
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)
Maybe it's the coding problem
You can try to add following code at the top of your code
# -*- coding: utf-8 -*-
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.