<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脚本将目录中的所有文件连接到一个文件中

        python script to concatenate all the files in the directory into one file(python脚本将目录中的所有文件连接到一个文件中)

        <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脚本将目录中的所有文件连接到一个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

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

                    1. <tfoot id='7PfAV'></tfoot>
                      • <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>
                              <tbody id='7PfAV'></tbody>