上面的代码很好,但是第二部分试图创建一个新的txt文件,其中包含有关在第一部分中创建的文件的信息,对于每个文件,例如,将写入该txt文件:INPUT FILE1 SIZE IS 42,OUTPUT FILE1 SIZE IS 324,比第二个文件:INPUT FILE2 SIZE IS 62,OUTPUT FILE1 SIZE IS 543 ...等等等
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()
答案 0 :(得分:2)
使用os.path.splitext
删除原始文件的扩展名,也不需要f.close()
,因为with
会自动关闭文件:
import glob, os
import pandas as pd
files = glob.glob('*.csv')
#loop by all files
for file in files:
if not file.startswith(('output_','file_size_')):
#for write to parameter w
with open(os.path.splitext(file)[0] + "stats.txt", 'w') as f:
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))
f.write(infile)
f.write(outfile)
编辑:
解决方案需要sum
的输入和输出文件到变量:
import glob, os
import pandas as pd
files = glob.glob('*.csv')
input_all, output_all = 0, 0
#loop by all files
for file in files:
if not (file.startswith('output_') or file.endswith('stats.txt')):
with open(os.path.splitext(file)[0] + "stats.txt", 'w') as f:
output_file_name = "output_" + file
#add both format
i = os.path.getsize(file)
o = os.path.getsize(output_file_name)
input_all += i
output_all += o
infile = 'SIZE OF INPUT FILE {} IS {}, '.format(file, i)
outfile = 'SIZE OF INPUT FILE {} IS {}'.format(output_file_name, o)
f.write(infile)
f.write(outfile)
with open("final_stats.txt", 'w') as f:
instring = 'SIZE OF ALL INPUT FILES IS {}, '.format(input_all)
outstring = 'SIZE OF ALL OUTPUT FILES IS {}, '.format(output_all)
both = 'SIZE OF ALL FILES IS {}'.format(input_all + output_all)
f.write(instring)
f.write(outstring)
f.write(both)
答案 1 :(得分:1)
更好的方法是将输出文件与输入文件放在不同的目录中,以免造成混淆。
data/
- input
- output
- stats
您还需要1次循环,而不是循环两次文件。
使用pathlib.Path
和f字符串(python 3.6及更高版本)将变为:
from pathlib import Path
DATA_DIR = Path("<data>") # fill in the correct path
INPUT_DIR = DATA_DIR / "input"
OUTPUT_DIR = DATA_DIR / "output"
STATS_DIR = DATA_DIR / "stats"
for file in INPUT_DIR.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 = [f'{a} of the {b}. column is {c}' for (a, b), c in s.items()]
output_file = OUTPUT_DIR / ("output_" + file.name)
pd.Series(L).to_csv(output_file ,index=False)
size_input = file.stat().st_size
size_output = output_file.stat().st_size
stats_file = STATS_DIR / file.with_suffix("stats.txt").name
message = f"""input file size is {size_input}
output file size is {size_output}"""
stats_file.write_text(message)