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



上面的代码部分很好,但是第二部分我试图创建一个新的txt文件,其中包含有关在第一部分中创建的文件的信息,例如在此txt文件中将被写入: 输入文件1大小为42,输出文件1大小为324,比第二个文件:输入文件2大小为62, 输出文件1大小为543等

进口大熊猫作为PD

进口球

导入操作系统

files = 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)

最新更新