vdu16
vdu16

Reputation: 145

Combine multiple text files into only one text file

I would like to combine multiple text files into only one text file, and read all the contents in it. However, the codes read only one text file and save data taken from only one text file. The codes;

path = "/home/Documents/Python/"
     
read_files = glob.glob(path+"*.txt")

with open("contents.txt", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            outfile.write(infile.read())

I also tried nearly same another code. However, it couldn't convert string to float because the first rows in the text files contain # character.

x = np.array([float(raw) for raw in f.readlines()])

I am aware that there are also some other questions in stackoverflow, highlighting how to do it. Even though I tried them as well, I couldn't achieve it properly.

I will appreciate any help or suggestion.

Upvotes: 1

Views: 2702

Answers (2)

ASH
ASH

Reputation: 20302

Something like this, perhaps.

import os
import glob
import pandas as pd
os.chdir("C:\\")


extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]


#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_csv.to_csv( "C:\\combined_csv.csv", index=False, encoding='utf-8-sig')

Upvotes: 0

vdu16
vdu16

Reputation: 145

in case someone wants to use another method, these codes have worked for me. i have followed another path to find out a solution for my own question.

all_files = glob.glob(path + "/*.txt")
all = []
for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    all.append(df)
frame = pd.concat(all, axis=0, ignore_index=True)

Upvotes: 2

Related Questions