0

Python Newbie here, First time poster:

Attempting to combine csvs. I have put all the files in one folder. Attempting to combine them into 1 csv.

from os import chdir
from glob import glob
import pandas as pdlib

# Produce a single CSV after combining all files
def produceOneCSV(list_of_files, file_out):
   # Consolidate all CSV files into one object
   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_of_files])
   # Convert the above object into a csv file and export
   result_obj.to_csv(file_out, index=False, encoding="utf-8")

# Move to the path that holds our CSV files
csv_file_path = 'C:/GolfPython/'
chdir(csv_file_path)
print(csv_file_path)
# List all CSV files in the working dir
file_pattern = ".csv"
list_of_files = [file for file in glob('*.{}'.format(file_pattern))]
print(list_of_files)

file_out = "ConsolidateOutput.csv"
produceOneCSV(list_of_files, file_out)`

Used code I found online.

Received the following error: ** File "C:\Users\dsitar\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\concat.py", line 262, in init raise ValueError('No objects to concatenate')

ValueError: No objects to concatenate**

2 Answers 2

1

You are using file_pattern = ".csv" and then putting another dot(.) in glob('*.{}'.format(file_pattern))

So, your program is searching for files with format ..csv which obviously don't exist.

To fix it, you can do one of the following

1.) Change file_pattern to "csv" (without dot) OR
2.) Change string formatting to '*{}'.format(file_pattern)' (No Dot in string)

Sign up to request clarification or add additional context in comments.

Comments

0

The issue is here:

list_of_files = [file for file in glob('*.{}'.format(file_pattern))]

Needs to be:

list_of_files = [file for file in glob('*{}'.format(file_pattern))]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.