【发布时间】:2019-08-31 10:01:30
【问题描述】:
我有一个文件夹,其中有一个子目录。在该子目录中,我想创建多个子目录。每次我运行代码而不删除现有代码时。如果发现存在重复的子目录,则保留旧的子目录,而不是为相同的子目录创建新的
我的代码目前在主目录中创建了一个子目录,但每次我运行我的代码时,我都必须删除现有的主子目录,否则代码不起作用。下面是我的代码
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,否则我的代码会失败。我想继续在 NEW_VIDEOS 内添加新的子目录而不删除现有的子目录,如果子目录已经存在,那么它应该只为新数据创建子目录并保持旧数据不变。可以做哪些改变来达到预期的效果?
【问题讨论】:
-
什么意思是“代码不起作用”。 ?如果您收到错误消息,请添加有问题的消息。始终将完整的错误消息(Traceback)放在有问题的地方。
标签: python directory subdirectory