<p>我对Python还不熟悉。
我的目录中有两个文件text1.txt和text2.txt。我需要合并text1和text2的数据并将结果打印在结果.txt. 你知道吗</p>
<p>例如:</p>
<p>文本1.txt</p>
<pre><code>a
b
c
</code></pre>
<p>文本2.txt</p>
<pre><code>aa bb
cc
</code></pre>
<p>你知道吗结果.txt你知道吗</p>
<pre><code>a
b
c
aa
bb
cc
</code></pre>
<p>(一个比一个低)</p>
<p>现在,我知道了一个脚本,通过声明全局</p>
<pre><code>import glob
files = glob.glob( '*.txt' )
with open( 'result.txt', 'w' ) as result:
for file_ in files:
for line in open( file_, 'r' ):
result.write( line )
</code></pre>
<p>但是我有很多以.txt结尾的文件,我想要特定的文件。你知道吗</p>
<p>任何帮助都将不胜感激。你知道吗</p>
<p>只需创建一个要从中复制的文件列表。你知道吗</p>
<pre><code>files = ['text1.txt', 'text2.txt']
with open('result.txt', 'w') as result:
for file_ in files:
for line in open(file_, 'r'):
result.write(line)
</code></pre>