Stack multiple txt files using pandas

0

I wanted to do a routine that accumulates the data I extract from google analytics every day, but my code has an error that I can not solve:

import pandas as pd
import os
import glob

my_dir = 'N:/E-Commerce/Relatorios/Bases/Source/Canais'
filelist = []
filesList = []
os.chdir(my_dir)
for files in glob.glob('*.txt'):
    fileName, fileExtension = os.path.splitext(files)
    filelist.append(fileName) 
    filesList.append(files) 
df = pd.DataFrame()
for f in filelist:
    frame = pd.read_csv(f)
    df = df.append(frame)
print(df)

It presents an error that can not find file 'b 20180401':

FileNotFoundError: File b'20180401 'does not exist

However, the file only has a name of 20180401 (one file per day).

    
asked by anonymous 26.06.2018 / 23:46

1 answer

0

The list under which you are iterating is the list that contains the names of the files without the extension .txt logo, these files do not really exist in your path.

Try changing:

for f in filelist:
    ....

By

for f in filesList:
    ....

That should work as expected, after all, it is in this list that contains the names of the complete archives (with extension).

Another point (which does not interfere with the operation of your code), try to give a name that makes sense for your variants, in this case it is very easy to confuse filelist with filesList even the two content tents and maybe different purposes .

I hope I have helped: D

    
27.06.2018 / 14:54