擅长:python、mysql、java
<p>响应是<a href="https://stackoverflow.com/questions/13613336/python-concatenate-text-files">already here</a>:</p>
<pre><code>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)
</code></pre>
<h3>扁平线溶液</h3>
<p>你需要的(根据评论),是一个只有两行的文件。在第一行,第一个文件的内容(没有换行符),在第二行,第二个文件。所以,如果你的文件很小(每个文件小于~1MB,在它占用大量内存之后…)</p>
<pre><code>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)
</code></pre>