Stack several txt files using pandas

Asked

Viewed 826 times

0

I wanted to make a routine that accumulates the data I extract from google Analytics every day, but my code shows 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 displays an error that cannot find the file 'b 20180401':

Filenotfounderror: File b'20180401' does not exist

however, the file has name only 20180401 (one file per day).

1 answer

0


The list under which you are iterating, is the list that contains the names of the files without the extension .txt so, these files don’t really exist on your path.

Try to change:

for f in filelist:
    ....

For

for f in filesList:
    ....

Which should work as expected, after all, is in this list that contains the full arches names (with extension).

Another point (which does not interfere in the functioning of your code), try to give name that make sense to your variables, in this case it is very easy to confuse filelist with filesList even the two tents maintenance and talves different purposes.

I hope I helped :D

  • You did, all right! but I still haven’t been able to create the DF because it gives me the error Unicodedecodeerror: 'utf-8' codec can’t Decode byte 0xff in position 0: invalid start byte so I looked up a python problem in iterating . txt knows how to proceed in this case ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.