Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件夹中的所有文件合并到一个文件中_Python_Shutil_Os.path - Fatal编程技术网

Python 将文件夹中的所有文件合并到一个文件中

Python 将文件夹中的所有文件合并到一个文件中,python,shutil,os.path,Python,Shutil,Os.path,我在一个文件夹中有大量文件(大约200万个文件),我想将每50个文件合并成一个。下面的代码将所有内容合并为一个。我无法找到一种方法来将每50个文件合并成一个文件,也无法确保50的乘法中的任何数字也合并成一个文件。例如,如果文件的数量是2000034,那么我最终得到的是50个组合文件的多个文件和一个包含最后34个文件的文件 from glob import iglob import shutil import os PATH = r'C:\Test' destination = open('a

我在一个文件夹中有大量文件(大约200万个文件),我想将每50个文件合并成一个。下面的代码将所有内容合并为一个。我无法找到一种方法来将每50个文件合并成一个文件,也无法确保50的乘法中的任何数字也合并成一个文件。例如,如果文件的数量是2000034,那么我最终得到的是50个组合文件的多个文件和一个包含最后34个文件的文件

from glob import iglob
import shutil
import os

PATH = r'C:\Test'

destination = open('allcontents.sh', 'wb')
for file in iglob(os.path.join(PATH, '*.sh')):
    shutil.copyfileobj(open(file, 'rb'), destination)
destination.close()

我会使用列表而不是迭代器,因为列表更容易操作:

filelist = glob(os.path.join(PATH, '*.sh'))
将列表切分为50个项目片段,并将每个片段中的文件复制到一个文件中。输出文件的名称包含切片中第一个文件的编号:

BLOCK = 50
for i in range(0, len(filelist) + BLOCK, BLOCK):
    with open('contents-{}.sh'.format(i), 'wb') as destination:
        for filename in filelist[i:i+BLOCK]:
            with open(filename, 'rb') as infile:
                shutil.copyfileobj(infile, destination)

我会使用列表而不是迭代器,因为列表更容易操作:

filelist = glob(os.path.join(PATH, '*.sh'))
将列表切分为50个项目片段,并将每个片段中的文件复制到一个文件中。输出文件的名称包含切片中第一个文件的编号:

BLOCK = 50
for i in range(0, len(filelist) + BLOCK, BLOCK):
    with open('contents-{}.sh'.format(i), 'wb') as destination:
        for filename in filelist[i:i+BLOCK]:
            with open(filename, 'rb') as infile:
                shutil.copyfileobj(infile, destination)

你差不多有这个了。我还没有测试下面的代码,但它会告诉您:

from glob import iglob
import shutil
import os

PATH = r'C:\Test'

filecounter = 1
fiftycounter = 0
destination = open('fifties1.sh', 'wb')
for file in iglob(os.path.join(PATH, '*.sh')):
    shutil.copyfileobj(open(file, 'rb'), destination)
    fiftycounter += 1
    if 50 == fiftycounter:
        fiftycounter = 0
        destination.close()
        filecounter += 1
        destination = open('fifties' + str(filecounter) + '.sh', 'wb')
destination.close()

你差不多有这个了。我还没有测试下面的代码,但它会告诉您:

from glob import iglob
import shutil
import os

PATH = r'C:\Test'

filecounter = 1
fiftycounter = 0
destination = open('fifties1.sh', 'wb')
for file in iglob(os.path.join(PATH, '*.sh')):
    shutil.copyfileobj(open(file, 'rb'), destination)
    fiftycounter += 1
    if 50 == fiftycounter:
        fiftycounter = 0
        destination.close()
        filecounter += 1
        destination = open('fifties' + str(filecounter) + '.sh', 'wb')
destination.close()
标准图书馆文档中的内容包括此配方(逐字引用):

您可以使用它来包装
iglob()
调用,以返回50个元素的块(最后一个元素的末尾将有额外的
None
),然后遍历该列表

i_files = iglob(os.path.join(PATH, '*.sh'))
i_grouped = grouper(i_files, 50)
for (n, group) in enumerate(i_grouped):
  destination_fn = 'allcontents_{}.sh'.format(n)
  with open(destination_fn, 'w') as f:
    for input_fn in group:
      ...
标准图书馆文档中的内容包括此配方(逐字引用):

您可以使用它来包装
iglob()
调用,以返回50个元素的块(最后一个元素的末尾将有额外的
None
),然后遍历该列表

i_files = iglob(os.path.join(PATH, '*.sh'))
i_grouped = grouper(i_files, 50)
for (n, group) in enumerate(i_grouped):
  destination_fn = 'allcontents_{}.sh'.format(n)
  with open(destination_fn, 'w') as f:
    for input_fn in group:
      ...