如何从一个文件夹中一次运行五个文件中的批处理文件?

时间:2018-12-23 23:56:41

标签: python python-3.x file directory

我有一个大约100个文件的文件夹。我想一次运行5个文件。对于前5个文件,我想运行一个函数my_func,然后对接下来的5个(6..10)文件等运行。

my_func所做的是将文件组合在一起。因此,我想结合前5个文件将它们写入目录,然后转到下5个文件。

这是我到目前为止所做的,

file_list = os.listdir(read_path)
len(file_list)

for file in range(len(file_list)):
    if file <= 4:
        my_func()
    ## Not sure How to go to Next Five after runing my_func

任何有关完成此代码的帮助都将非常有用。

1 个答案:

答案 0 :(得分:2)

您可以使用内置的import numpy as np a = np.random.rand(10, 3) b = np.random.rand(10, 3, 2) c = b * a[..., np.newaxis] # make a shape 10 x 3 x 1 和一个步骤来获取每5个文件的索引,然后相应地对列表进行切片。

range

num_files_per = 5 for file_index in range(0, len(file_list), num_files_per): file_batch = file_list[file_index : (file_index+num_files_per)] # Slice a batch of 5 files my_func(file_batch) # Run your function on batch of 5 files 中的第3个参数将决定步长,默认情况下为1。