pythonpandasfilegetopenfilename

How to make a unique dataframe from multiple .txt files?


I'm trying to make a data frame from multiple .txt files. It may be important to say, those .txt files used to be .dat files, which I've just converted by changing the .dat to .txt. Anyway, it opens normally with a double click.

My code is:

path = "/home/marlon/Shift One/Projeto Philips/Consolidação de Arquivos/dados/17448"

files = []
for i in os.listdir(path):
    if os.path.isfile(os.path.join(path,i)):
        files.append(i)

df = pd.DataFrame()
for i in files:
    frame = pd.read_csv(i)
    df = df.append(frame)
print(df)

The output is:

...

  File "pandas/_libs/parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__

  File "pandas/_libs/parsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source

FileNotFoundError: File b'monitor_System_HumTechRoom.txt' does not exist

Thanks for your help!


Solution

  • Your list "files" contains only the contents of the directory "path" instead of the absolute paths to the files in there. Pandas are looking for "monitor_System_HumTechRoom.txt" instead of "/home/marlon/Shift One/Projeto Philips/Consolidação de Arquivos/dados/17448/monitor_System_HumTechRoom.txt" hence the FileNotFoundError. Do I understand correctly that you want to read all files from a directory as csvs into a single dataframe and then print the dataframe?

    No need to loop twice. Try something like this:

    csvdir = "/home/marlon/Shift One/Projeto Philips/Consolidação de Arquivos/dados/17448"
    
    dataframes = []
    for csv in os.listdir(csvdir):
        fullpath = os.path.join(csvdir, csv)
        if os.path.isfile(fullpath):
            # Read a csv into a dataframe and append to a list of dataframes
            dataframe = pd.read_csv(fullpath)
            dataframes.append(dataframe)
    
    # Concatenate all created dataframes into one
    df = pd.concat(dataframes)
    print(df)