Python 添加新子目录而不删除现有子目录

Python 添加新子目录而不删除现有子目录,python,directory,subdirectory,Python,Directory,Subdirectory,我有一个文件夹,其中有一个子目录。在该子目录中,我想创建多个子目录。每次我运行代码时都不会删除现有的代码。如果发现存在重复的子目录,则保留旧的子目录,而不是为相同的子目录创建新的子目录 我的代码当前在主目录中创建一个子目录,但每次运行代码时,我都必须删除现有的主子目录,否则代码将无法工作。下面是我的代码 def extractFrames(m,n): if not os.path.exists(n): os.makedirs(n) vid_files=glob

我有一个文件夹,其中有一个子目录。在该子目录中,我想创建多个子目录。每次我运行代码时都不会删除现有的代码。如果发现存在重复的子目录,则保留旧的子目录,而不是为相同的子目录创建新的子目录

我的代码当前在主目录中创建一个子目录,但每次运行代码时,我都必须删除现有的主子目录,否则代码将无法工作。下面是我的代码

def extractFrames(m,n):

    if not os.path.exists(n):
        os.makedirs(n)

    vid_files=glob(m)
    print(vid_files)

    for v_f in range(len(vid_files)):
        v1=os.path.basename(vid_files[v_f])
        print(v1)
        vid_name = os.path.splitext(v1)[0]
        print(vid_name)
        output = n +'\\video_' + vid_name
        os.makedirs(output)
        print(output)

        print(vid_files[v_f])


        vidcap = cv2.VideoCapture(vid_files[v_f])
        print(vidcap)
        success,image = vidcap.read()
        seconds = 1
        fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
        multiplier = fps * seconds
        count=0

        while success:
            img_name = vid_name + '_f' + str(count) + ".jpg"
            image_path = output + "/" + img_name
            frameId = int(round(vidcap.get(1)))
            success,image = vidcap.read()
            if frameId % multiplier == 0:
                cv2.imwrite(filename = image_path, img = image)
                count+=1


        vidcap.release()
        cv2.destroyAllWindows()

        print('finished processing video {0} with frames {1}'.format(vid_files[v_f], count))
    return output


x=("C:\\Python36\\videos\\*.mp4")
y=("C:\\Python36\\videos\\videos_new")

我有一个名为VIDEOS的目录。我的代码创建了一个名为NEW_VIDEOS的子目录,其中包含多个子目录。每次在运行代码之前,我都必须删除新的视频,否则我的代码就会失败。我想继续在新的视频中添加新的子目录,而不删除现有的子目录,如果子目录已经存在,那么它应该只为新数据创建子目录,并保持旧的子目录不变。可以进行哪些更改以实现所需的结果?

添加
在所有
os.makedirs(…)

文档:

什么是“代码不工作”?如果您收到错误消息,请将其添加到问题中。始终对完整的错误消息(回溯)提出疑问。
 os.makedirs(path, exist_ok=True)