Make new txt file with size info of output and input files

时间:2019-01-07 12:58:44

标签: python-3.x pandas dataframe glob

The above part of the code is good, but the second part im trying to create a new txt file with information about files that are created in the first part, for example in this txt file will be written: INPUT FILE1 SIZE IS 42, OUTPUT FILE1 SIZE IS 324, than the second file: INPUT FILE2 SIZE IS 62, OUTPUT FILE1 SIZE IS 543...etc etc

import pandas as pd

import glob

import os

files = glob.glob('*.csv')

for file in files:

df = pd.read_csv(file, header= None)

df1 = df.iloc[:, :4].agg(['sum','max','std'])

df1.columns = range(1, len(df1.columns) + 1)

s = df1.stack()

L = ['{} of the {}. column is {}'.format(a, b, c) for (a, b), c in s.items()]

output_file_name = "output_" + file

pd.Series(L).to_csv(output_file_name ,index=False)#this part is good

for file in files:

with open(file + "stats.txt", 'a+') as f:

    f.write(' input file size is {}'.format(os.path.getsize(file)))

f.write('output file size is {}'.format(os.path.getsize(output_file_name)))

f.close()

1 个答案:

答案 0 :(得分:1)

Use:

import glob, os
import pandas as pd

files = glob.glob('*.csv')

#loop by all files
for file in files:
    L = []
    #remove not starting by output_
    if not file.startswith(('output_','file_size_')):
        output_file_name = "output_" + file
        #add both format
        infile = 'SIZE OF INPUT FILE {} IS {}, '.format(file, os.path.getsize(file))
        outfile = 'SIZE OF INPUT FILE {} IS {}'.format(output_file_name, 
                                                       os.path.getsize(output_file_name))
        #join together and append to list
        L.append(infile + outfile )

        #create Series and write to file
        pd.Series(L).to_csv('file_size_{}'.format(file), index=False)