鱼C论坛

 找回密码
 立即注册
查看: 1497|回复: 12

[已解决]第30讲第三题

[复制链接]
发表于 2020-4-14 14:13:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我自己写的是,但是感觉不好运作,我自己的思路是只要一个参数就是路径,然后每次遍历时写入videolist文件,写完再关闭,但是保存位置也不确定在哪,之后再递归。 但是总感觉自己摸不透自己的问题在哪? 麻烦大家看看了。谢谢了!

  1. import os
  2. ext = ['.mp4','.rmvb','.avi']
  3. def search_video(file_path):
  4.          allfiles = os.listdir(file_path)
  5.          for eachfile in allfiles:
  6.              if os.path.splitext(eachfile)[1] in ext:
  7.                  f = open('videolist.txt','w')
  8.                  f.write(str(os.path.dirname(eachfile) + '\\' +str(os.path.basename(eachfile))))
  9.                  f.close()
  10.              if os.path.isfile(eachfile):
  11.                  search_video(eachfile)
  12.                  os.chdir(os.pardir)

  13. filepath = input('路径:')
  14. search_video(filepath)
复制代码
答案是

  1. import os

  2. def search_file(start_dir, target) :
  3.     os.chdir(start_dir)
  4.    
  5.     for each_file in os.listdir(os.curdir) :
  6.         ext = os.path.splitext(each_file)[1]
  7.         if ext in target :
  8.             vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) # 使用os.sep是程序更标准
  9.         if os.path.isdir(each_file) :
  10.             search_file(each_file, target) # 递归调用
  11.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录

  12. start_dir = input('请输入待查找的初始目录:')
  13. program_dir = os.getcwd()

  14. target = ['.mp4', '.avi', '.rmvb']
  15. vedio_list = []

  16. search_file(start_dir, target)

  17. f = open(program_dir + os.sep + 'vedioList.txt', 'w')
  18. f.writelines(vedio_list)
  19. f.close()
复制代码

我可以理解答案是把采集到的文件名存在列表里最后再写入txt,但是txt文件保存位置是哪?


最佳答案
2020-4-14 17:37:25
本帖最后由 txxcat 于 2020-4-14 17:52 编辑

先说几个明显得错误:
  1. import os
  2. ext = ['.mp4','.rmvb','.avi']
  3. def search_video(file_path):
  4.          allfiles = os.listdir(file_path)
  5.          for eachfile in allfiles:
  6.              if os.path.splitext(eachfile)[1] in ext:
  7.                  f = open('videolist.txt','w')            #用w打开文件都会覆盖掉老文件,而且,没有绝对路径名,在每个符合条件的目录下都生成一个文件,乱!
  8.                  f.write(str(os.path.dirname(eachfile) + '\\' +str(os.path.basename(eachfile))))
  9.                  f.close()
  10.              if os.path.isfile(eachfile):                #不该是os.path.isdir ???
  11.                  search_video(eachfile)
  12.                  os.chdir(os.pardir)                    #之前又没有chdir,现在chdir,混乱了

  13. filepath = input('路径:')
  14. search_video(filepath)
复制代码


再修正一下代码:
  1. import os
  2. ext = ['.mp4','.rmvb','.avi']
  3. def search_video(file_path):
  4.     allfiles = os.listdir('.')                    #已在目标目录,涉及到递归调用,不能使用目录名
  5.     for eachfile in allfiles:
  6.         if os.path.splitext(eachfile)[1] in ext:
  7.             f = open(cdir+'\\'+'videolist.txt','a')                                               #要使用绝对路径名
  8.             f.write(os.getcwd() + '\\' +str(os.path.basename(eachfile))+'\n')      #os.getcwd()才能获取完整的路径     
  9.             f.close()
  10.         if os.path.isdir(eachfile):
  11.             os.chdir(eachfile)               #转到新的目标目录
  12.             search_video(eachfile)        #递归调用
  13.             os.chdir(os.pardir)              #回到上一级目录

  14. filepath = input('路径:')
  15. cdir=os.getcwd()           #记录工作目录
  16. os.chdir(filepath)           #去到目标目录
  17. search_video(filepath)
  18. os.chdir(cdir)                #回到工作目录
复制代码

代码是可以跑了,不过还有些不合理的地方,比如你每发现一个视频文件,就打开一次写入再关闭,动作频繁,应该程序开始就打开,然后发现写入,程序结束时关闭才合理:
  1. def search_video(file_path):
  2.     allfiles = os.listdir('.')
  3.     for eachfile in allfiles:
  4.         if os.path.splitext(eachfile)[1] in ext:
  5.             f.write(os.getcwd() + '\\' +str(os.path.basename(eachfile))+'\n')
  6.         if os.path.isdir(eachfile):
  7.             os.chdir(eachfile)
  8.             search_video(eachfile)
  9.             os.chdir(os.pardir)

  10. import os
  11. ext = ['.mp4','.rmvb','.avi']
  12. f = open('videolist.txt','w')     #这回可以用w,保证每次写入的是新数据
  13. filepath = input('路径:')
  14. cdir=os.getcwd()
  15. os.chdir(filepath)
  16. search_video(filepath)
  17. os.chdir(cdir)
  18. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-14 14:16:20 | 显示全部楼层
默认和代码一个目录
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-14 14:17:15 | 显示全部楼层
这道题自己做的有点晕 隔了两天没复习os模块的指令 就有点不知所措了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-14 14:17:55 | 显示全部楼层
在程序代码的同一个目录下,如果指定位置保存可以在文件名前加上文件路径
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-14 14:20:31 | 显示全部楼层
zhongyuanadler 发表于 2020-4-14 14:17
这道题自己做的有点晕 隔了两天没复习os模块的指令 就有点不知所措了

那你就收藏一下吧 --> https://fishc.com.cn/thread-45512-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-14 14:22:24 | 显示全部楼层
wuqramy 发表于 2020-4-14 14:20
那你就收藏一下吧 --> https://fishc.com.cn/thread-45512-1-1.html

嗯 谢谢 我是开着这个扩展阅读 一边看一边做的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-14 14:22:56 | 显示全部楼层
老八秘制 发表于 2020-4-14 14:16
默认和代码一个目录

恩恩 谢谢,有时间看看我的代码的问题是什么吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-14 14:28:12 | 显示全部楼层
zhongyuanadler 发表于 2020-4-14 14:22
恩恩 谢谢,有时间看看我的代码的问题是什么吗?

你发现了什么问题呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-14 14:39:50 | 显示全部楼层
wuqramy 发表于 2020-4-14 14:28
你发现了什么问题呢

跑了一下代码,输入路径什么反应都没有,而且文件都没创建
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-14 14:49:34 | 显示全部楼层
zhongyuanadler 发表于 2020-4-14 14:22
恩恩 谢谢,有时间看看我的代码的问题是什么吗?

你的代码缩进有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-14 15:05:52 | 显示全部楼层

改成这样了:
  1. import os
  2. ext = ['.mp4','.rmvb','.avi']
  3. def search_video(file_path):
  4.     allfiles = os.listdir(file_path)
  5.     for eachfile in allfiles:
  6.         if os.path.splitext(eachfile)[1] in ext:
  7.           f = open('videolist.txt','w')
  8.           f.write(str(os.path.dirname(eachfile) + '\\' +str(os.path.basename(eachfile))))
  9.           f.close()
  10.         if os.path.isfile(eachfile):
  11.           search_video(eachfile)
  12.           os.chdir(os.pardir)

  13. filepath = input('路径:')
  14. search_video(filepath)
复制代码


不过感觉没什么区别啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-14 17:37:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 txxcat 于 2020-4-14 17:52 编辑

先说几个明显得错误:
  1. import os
  2. ext = ['.mp4','.rmvb','.avi']
  3. def search_video(file_path):
  4.          allfiles = os.listdir(file_path)
  5.          for eachfile in allfiles:
  6.              if os.path.splitext(eachfile)[1] in ext:
  7.                  f = open('videolist.txt','w')            #用w打开文件都会覆盖掉老文件,而且,没有绝对路径名,在每个符合条件的目录下都生成一个文件,乱!
  8.                  f.write(str(os.path.dirname(eachfile) + '\\' +str(os.path.basename(eachfile))))
  9.                  f.close()
  10.              if os.path.isfile(eachfile):                #不该是os.path.isdir ???
  11.                  search_video(eachfile)
  12.                  os.chdir(os.pardir)                    #之前又没有chdir,现在chdir,混乱了

  13. filepath = input('路径:')
  14. search_video(filepath)
复制代码


再修正一下代码:
  1. import os
  2. ext = ['.mp4','.rmvb','.avi']
  3. def search_video(file_path):
  4.     allfiles = os.listdir('.')                    #已在目标目录,涉及到递归调用,不能使用目录名
  5.     for eachfile in allfiles:
  6.         if os.path.splitext(eachfile)[1] in ext:
  7.             f = open(cdir+'\\'+'videolist.txt','a')                                               #要使用绝对路径名
  8.             f.write(os.getcwd() + '\\' +str(os.path.basename(eachfile))+'\n')      #os.getcwd()才能获取完整的路径     
  9.             f.close()
  10.         if os.path.isdir(eachfile):
  11.             os.chdir(eachfile)               #转到新的目标目录
  12.             search_video(eachfile)        #递归调用
  13.             os.chdir(os.pardir)              #回到上一级目录

  14. filepath = input('路径:')
  15. cdir=os.getcwd()           #记录工作目录
  16. os.chdir(filepath)           #去到目标目录
  17. search_video(filepath)
  18. os.chdir(cdir)                #回到工作目录
复制代码

代码是可以跑了,不过还有些不合理的地方,比如你每发现一个视频文件,就打开一次写入再关闭,动作频繁,应该程序开始就打开,然后发现写入,程序结束时关闭才合理:
  1. def search_video(file_path):
  2.     allfiles = os.listdir('.')
  3.     for eachfile in allfiles:
  4.         if os.path.splitext(eachfile)[1] in ext:
  5.             f.write(os.getcwd() + '\\' +str(os.path.basename(eachfile))+'\n')
  6.         if os.path.isdir(eachfile):
  7.             os.chdir(eachfile)
  8.             search_video(eachfile)
  9.             os.chdir(os.pardir)

  10. import os
  11. ext = ['.mp4','.rmvb','.avi']
  12. f = open('videolist.txt','w')     #这回可以用w,保证每次写入的是新数据
  13. filepath = input('路径:')
  14. cdir=os.getcwd()
  15. os.chdir(filepath)
  16. search_video(filepath)
  17. os.chdir(cdir)
  18. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-15 09:26:17 | 显示全部楼层
txxcat 发表于 2020-4-14 17:37
先说几个明显得错误:

感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 14:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表