import glob import os src_dir = '/root/*.txt' # 利用通配符查找后缀名为txt的文件 dest_file = 'result.txt' with open(dest_file, 'w') as f_w: for file_name in glob.glob(src_dir): with open(file_name) as f_r: for line in f_r: f_w.write('%s %s' % (file_name, line)) os.remove(file_name)
python2.7语法, py3请自行相应改下