filenames = ['file1.txt', 'file2.txt', ...]
with open('result.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
filenames = ['file1.txt', 'file2.txt', ...]
with open('result.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
content = infile.read().replace('\n', '')
outfile.write(content)
响应是already here:
扁平线溶液
你需要的(根据评论),是一个只有两行的文件。在第一行,第一个文件的内容(没有换行符),在第二行,第二个文件。所以,如果你的文件很小(每个文件小于~1MB,在它占用大量内存之后…)
如果您对Python不是特别感兴趣,那么UNIX
cat
命令的作用正好是:连接多个文件的内容。如果要在两个文件之间换行,请将第二行改为最后一行,使其具有
f1_contents + "\n" + f2_contents
。(\n
表示新行)。相关问题 更多 >
编程相关推荐