Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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_For Loop_Shutil - Fatal编程技术网

Python 多目录生成并将特定文件放入目录中

Python 多目录生成并将特定文件放入目录中,python,for-loop,shutil,Python,For Loop,Shutil,我编写了一段代码,使用python在这些文件夹中创建多个文件夹和特定文件 我有1920个图像,所有这些图像都与20个图像文件关联,命名为frame01、frame02、frame03。。。。image96(1帧包含20个图像文件) 如何创建新文件夹并将特定文件复制到已创建的文件夹中 创建目录的步骤 if not os.path.exists(directory): os.makedirs(directory) 复制文件 from shutil import copyfile copyf

我编写了一段代码,使用python在这些文件夹中创建多个文件夹和特定文件

我有1920个图像,所有这些图像都与20个图像文件关联,命名为
frame01、frame02、frame03。。。。image96
(1帧包含20个图像文件)

如何创建新文件夹并将特定文件复制到已创建的文件夹中

创建目录的步骤

if not os.path.exists(directory):
    os.makedirs(directory)
复制文件

from shutil import copyfile
copyfile(src, dst)
只需创建一个循环来复制check和createdir,然后根据您的条件使用copyfile复制文件


代码中出现了什么错误?

要根据文件名的前几个字符(如“frame”或“image”)选择和复制文件,请参见以下内容:

import os
import shutil

exist_dir = 'exist/dir'
new_dir = 'new/dir'
for (dirpath, dirnames, filenames) in os.walk(os.path.join(exist_dir+os.sep)):    
    for filename in filenames:
        if filename.startswith('frame') or filename.startswith('image'): 
            folderandfile = os.sep.join([dirpath, filename])               
            folderandfilenew = os.sep.join([new_dir, filename])
            shutil.copy2(folderandfile, folderandfile1)