filenames = ['file1.txt','file2.txt', ...]withopen('path/to/output/file', 'w')as outfile:for fname in filenames:withopen(fname)as infile:for line in infile: outfile.write(line)
小文件合并
filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
d = {}
with open("file.txt") as f:
for line in f:
(key, val) = line.split()
d[int(key)] = val
my_dict = {}
with open("rest.txt", 'r') as f:
for line in f:
items = line.split()
key, values = items[0], items[1:]
my_dict[key] = values
print my_dict