使用输出和输入文件的大小信息制作新的 txt 文件

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

上面的代码部分很好,但是第二部分我试图创建一个新的 txt 文件,其中包含有关在第一部分中创建的文件的信息,例如在这个 txt 文件中将写入:INPUT FILE1 SIZE IS 42,OUTPUT FILE1 SIZE IS 324,比第二个文件:INPUT FILE2 SIZE IS 62,OUTPUT FILE1 SIZE IS 543...等等等等

导入 pandas 作为 pd

导入 glob

进口os

文件 = glob.glob('*.csv')

文件中的文件:

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

文件中的文件:

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()

使用:

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)