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

Add new subdirectories without deleting exisiting ones

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

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

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 的子目录,其中包含多个子目录。每次在 运行 之前,我必须删除 NEW_VIDEOS 代码,否则我的代码会失败。我想继续在 INSIDE NEW_VIDEOS 中添加新子目录而不删除现有子目录,如果子目录已经存在,那么它应该只为新数据创建子目录并保持旧数据不变。可以做哪些改变来达到预期的效果?

exist_ok=Trueos.makedirs(...)

 os.makedirs(path, exist_ok=True) 

文档:os.makedirs()