I have a folder in which there is a subdirectory. Inside that subdirectory i want to create multiple subdirectories. Everytime i run the code without deleting the existing ones. And if a duplicate subdirectory is found existing then keep the old subdirectory instead of creating a new for the same
My code currently makes a subdirectory inside the main directory but everytime i run my code i have to delete the existing main subdirectory else the code doesn't work. Below is my code
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")
I have a directory named VIDEOS. Inside that my code creates a subdirectory called NEW_VIDEOS which has multiple subdirectories inside. Everytime before i run the code i have to delete NEW_VIDEOS else my code fails. I want to keep on adding new subdirectories INSIDE NEW_VIDEOS without deleting the existing ones and if a subdirectory already exists then it should only create the subdirectory for the new data and keep the old one as it is. What changes can be done to achieve the desired results?
Add exist_ok=True
in all os.makedirs(...)
os.makedirs(path, exist_ok=True)
Documentation: os.makedirs()