<i id='ABTFf'><tr id='ABTFf'><dt id='ABTFf'><q id='ABTFf'><span id='ABTFf'><b id='ABTFf'><form id='ABTFf'><ins id='ABTFf'></ins><ul id='ABTFf'></ul><sub id='ABTFf'></sub></form><legend id='ABTFf'></legend><bdo id='ABTFf'><pre id='ABTFf'><center id='ABTFf'></center></pre></bdo></b><th id='ABTFf'></th></span></q></dt></tr></i><div id='ABTFf'><tfoot id='ABTFf'></tfoot><dl id='ABTFf'><fieldset id='ABTFf'></fieldset></dl></div>

    • <bdo id='ABTFf'></bdo><ul id='ABTFf'></ul>

    <small id='ABTFf'></small><noframes id='ABTFf'>

      1. <legend id='ABTFf'><style id='ABTFf'><dir id='ABTFf'><q id='ABTFf'></q></dir></style></legend>
        <tfoot id='ABTFf'></tfoot>

        python脚本将目录中的所有文件连接到一个文件中

        时间:2023-08-31

        <small id='z8s7k'></small><noframes id='z8s7k'>

              <tbody id='z8s7k'></tbody>

              <bdo id='z8s7k'></bdo><ul id='z8s7k'></ul>
              <tfoot id='z8s7k'></tfoot>

                <legend id='z8s7k'><style id='z8s7k'><dir id='z8s7k'><q id='z8s7k'></q></dir></style></legend>
                <i id='z8s7k'><tr id='z8s7k'><dt id='z8s7k'><q id='z8s7k'><span id='z8s7k'><b id='z8s7k'><form id='z8s7k'><ins id='z8s7k'></ins><ul id='z8s7k'></ul><sub id='z8s7k'></sub></form><legend id='z8s7k'></legend><bdo id='z8s7k'><pre id='z8s7k'><center id='z8s7k'></center></pre></bdo></b><th id='z8s7k'></th></span></q></dt></tr></i><div id='z8s7k'><tfoot id='z8s7k'></tfoot><dl id='z8s7k'><fieldset id='z8s7k'></fieldset></dl></div>

                1. 本文介绍了python脚本将目录中的所有文件连接到一个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have written the following script to concatenate all the files in the directory into one single file.

                  Can this be optimized, in terms of

                  1. idiomatic python

                  2. time

                  Here is the snippet:

                  import time, glob
                  
                  outfilename = 'all_' + str((int(time.time()))) + ".txt"
                  
                  filenames = glob.glob('*.txt')
                  
                  with open(outfilename, 'wb') as outfile:
                      for fname in filenames:
                          with open(fname, 'r') as readfile:
                              infile = readfile.read()
                              for line in infile:
                                  outfile.write(line)
                              outfile.write("
                  
                  ")
                  

                  解决方案

                  Use shutil.copyfileobj to copy data:

                  import shutil
                  
                  with open(outfilename, 'wb') as outfile:
                      for filename in glob.glob('*.txt'):
                          if filename == outfilename:
                              # don't want to copy the output into the output
                              continue
                          with open(filename, 'rb') as readfile:
                              shutil.copyfileobj(readfile, outfile)
                  

                  shutil reads from the readfile object in chunks, writing them to the outfile fileobject directly. Do not use readline() or a iteration buffer, since you do not need the overhead of finding line endings.

                  Use the same mode for both reading and writing; this is especially important when using Python 3; I've used binary mode for both here.

                  这篇关于python脚本将目录中的所有文件连接到一个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将具有特定文件扩展名的文件复制到我的 python(2.5 版)脚本中的文件夹中? 下一篇:克服 Python 关于实例方法的限制

                  相关文章

                  <small id='7PfAV'></small><noframes id='7PfAV'>

                    1. <tfoot id='7PfAV'></tfoot>
                    2. <i id='7PfAV'><tr id='7PfAV'><dt id='7PfAV'><q id='7PfAV'><span id='7PfAV'><b id='7PfAV'><form id='7PfAV'><ins id='7PfAV'></ins><ul id='7PfAV'></ul><sub id='7PfAV'></sub></form><legend id='7PfAV'></legend><bdo id='7PfAV'><pre id='7PfAV'><center id='7PfAV'></center></pre></bdo></b><th id='7PfAV'></th></span></q></dt></tr></i><div id='7PfAV'><tfoot id='7PfAV'></tfoot><dl id='7PfAV'><fieldset id='7PfAV'></fieldset></dl></div>

                      • <bdo id='7PfAV'></bdo><ul id='7PfAV'></ul>
                      <legend id='7PfAV'><style id='7PfAV'><dir id='7PfAV'><q id='7PfAV'></q></dir></style></legend>