鱼C论坛

 找回密码
 立即注册
查看: 1550|回复: 1

[已解决]《课后作业三十讲》第3小题对于自己所写答案的疑惑

[复制链接]
发表于 2021-2-6 17:14:08 | 显示全部楼层 |阅读模式

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

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

x
  1. import os

  2. def search(start_path):

  3.     os.chdir(start_path)

  4.     namelst = os.listdir(os.curdir)

  5.     extension = ['.mp4','.rmvb','.avi']

  6.     for each_file in namelst:

  7.         if os.path.isfile(each_file):

  8.             if os.path.splitext(each_file) in extension:

  9.                 f = open('D:\\vedioList.txt','x')

  10.                 final_path = start_path + each_file

  11.                 f.writelines(final_path)

  12.                 f.close()

  13.         if os.path.isdir(each_file):

  14.             search(each_file)

  15.             os.chdir(os.pardir)


  16. start_path = input('请输入待查找的初始目录:')

  17. search(start_path)

复制代码




在运行过后没有报错也没有结果,这是为什么呢?

还有一个小问题:小甲鱼的片名能搜到嘛?
最佳答案
2021-2-6 18:24:17
本帖最后由 jackz007 于 2021-2-6 18:33 编辑
  1. import os

  2. def search(start_path):
  3.     pwd = os . getcwd()
  4.     os . chdir(start_path)
  5.     cwd = os . getcwd()
  6.     extension = ['.mp4','.mkv' ,'.flv' , '.rmvb' , '.rm' , '.wmv' , '.avi']
  7.     try:
  8.         for each_file in os.listdir(os . curdir):
  9.             if os . path . isfile(each_file):
  10.                 if os . path . splitext(each_file)[1] . lower() in extension:
  11.                     f = open('D:\\vedioList.txt','a')
  12.                     final_path = os . path . join(cwd , each_file)
  13.                     f . write(final_path + '\n')
  14.                     f . close()
  15.             elif os . path . isdir(each_file):
  16.                 search(each_file)
  17.     except Exception as e:
  18.         print(e)
  19.     os . chdir(pwd)     
  20. start_path = input('请输入待查找的初始目录:')
  21. if os . path . exists('D:\\vedioList.txt') and  os . path . isfile('D:\\vedioList.txt'):
  22.     os . remove('D:\\vedioList.txt')
  23. search(start_path)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-6 18:24:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-2-6 18:33 编辑
  1. import os

  2. def search(start_path):
  3.     pwd = os . getcwd()
  4.     os . chdir(start_path)
  5.     cwd = os . getcwd()
  6.     extension = ['.mp4','.mkv' ,'.flv' , '.rmvb' , '.rm' , '.wmv' , '.avi']
  7.     try:
  8.         for each_file in os.listdir(os . curdir):
  9.             if os . path . isfile(each_file):
  10.                 if os . path . splitext(each_file)[1] . lower() in extension:
  11.                     f = open('D:\\vedioList.txt','a')
  12.                     final_path = os . path . join(cwd , each_file)
  13.                     f . write(final_path + '\n')
  14.                     f . close()
  15.             elif os . path . isdir(each_file):
  16.                 search(each_file)
  17.     except Exception as e:
  18.         print(e)
  19.     os . chdir(pwd)     
  20. start_path = input('请输入待查找的初始目录:')
  21. if os . path . exists('D:\\vedioList.txt') and  os . path . isfile('D:\\vedioList.txt'):
  22.     os . remove('D:\\vedioList.txt')
  23. search(start_path)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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