|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我自己写的是,但是感觉不好运作,我自己的思路是只要一个参数就是路径,然后每次遍历时写入videolist文件,写完再关闭,但是保存位置也不确定在哪,之后再递归。 但是总感觉自己摸不透自己的问题在哪? 麻烦大家看看了。谢谢了!
- import os
- ext = ['.mp4','.rmvb','.avi']
- def search_video(file_path):
- allfiles = os.listdir(file_path)
- for eachfile in allfiles:
- if os.path.splitext(eachfile)[1] in ext:
- f = open('videolist.txt','w')
- f.write(str(os.path.dirname(eachfile) + '\\' +str(os.path.basename(eachfile))))
- f.close()
- if os.path.isfile(eachfile):
- search_video(eachfile)
- os.chdir(os.pardir)
- filepath = input('路径:')
- search_video(filepath)
复制代码 答案是
- import os
- def search_file(start_dir, target) :
- os.chdir(start_dir)
-
- for each_file in os.listdir(os.curdir) :
- ext = os.path.splitext(each_file)[1]
- if ext in target :
- vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) # 使用os.sep是程序更标准
- if os.path.isdir(each_file) :
- search_file(each_file, target) # 递归调用
- os.chdir(os.pardir) # 递归调用后切记返回上一层目录
- start_dir = input('请输入待查找的初始目录:')
- program_dir = os.getcwd()
- target = ['.mp4', '.avi', '.rmvb']
- vedio_list = []
- search_file(start_dir, target)
- f = open(program_dir + os.sep + 'vedioList.txt', 'w')
- f.writelines(vedio_list)
- f.close()
复制代码
我可以理解答案是把采集到的文件名存在列表里最后再写入txt,但是txt文件保存位置是哪?
本帖最后由 txxcat 于 2020-4-14 17:52 编辑
先说几个明显得错误:
- import os
- ext = ['.mp4','.rmvb','.avi']
- def search_video(file_path):
- allfiles = os.listdir(file_path)
- for eachfile in allfiles:
- if os.path.splitext(eachfile)[1] in ext:
- f = open('videolist.txt','w') #用w打开文件都会覆盖掉老文件,而且,没有绝对路径名,在每个符合条件的目录下都生成一个文件,乱!
- f.write(str(os.path.dirname(eachfile) + '\\' +str(os.path.basename(eachfile))))
- f.close()
- if os.path.isfile(eachfile): #不该是os.path.isdir ???
- search_video(eachfile)
- os.chdir(os.pardir) #之前又没有chdir,现在chdir,混乱了
- filepath = input('路径:')
- search_video(filepath)
复制代码
再修正一下代码:
- import os
- ext = ['.mp4','.rmvb','.avi']
- def search_video(file_path):
- allfiles = os.listdir('.') #已在目标目录,涉及到递归调用,不能使用目录名
- for eachfile in allfiles:
- if os.path.splitext(eachfile)[1] in ext:
- f = open(cdir+'\\'+'videolist.txt','a') #要使用绝对路径名
- f.write(os.getcwd() + '\\' +str(os.path.basename(eachfile))+'\n') #os.getcwd()才能获取完整的路径
- f.close()
- if os.path.isdir(eachfile):
- os.chdir(eachfile) #转到新的目标目录
- search_video(eachfile) #递归调用
- os.chdir(os.pardir) #回到上一级目录
- filepath = input('路径:')
- cdir=os.getcwd() #记录工作目录
- os.chdir(filepath) #去到目标目录
- search_video(filepath)
- os.chdir(cdir) #回到工作目录
复制代码
代码是可以跑了,不过还有些不合理的地方,比如你每发现一个视频文件,就打开一次写入再关闭,动作频繁,应该程序开始就打开,然后发现写入,程序结束时关闭才合理:
- def search_video(file_path):
- allfiles = os.listdir('.')
- for eachfile in allfiles:
- if os.path.splitext(eachfile)[1] in ext:
- f.write(os.getcwd() + '\\' +str(os.path.basename(eachfile))+'\n')
- if os.path.isdir(eachfile):
- os.chdir(eachfile)
- search_video(eachfile)
- os.chdir(os.pardir)
- import os
- ext = ['.mp4','.rmvb','.avi']
- f = open('videolist.txt','w') #这回可以用w,保证每次写入的是新数据
- filepath = input('路径:')
- cdir=os.getcwd()
- os.chdir(filepath)
- search_video(filepath)
- os.chdir(cdir)
- f.close()
复制代码
|
|