鱼C论坛

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

第30讲的第四题

[复制链接]
发表于 2017-3-8 10:06:54 | 显示全部楼层 |阅读模式

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

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

x
  1. import os

  2. def vedio_file(start_file):

  3.     os.chdir(start_file)
  4.     all_file = os.listdir(os.curdir)
  5.     for each_file in all_file:
  6.         if os.path.splitext(each_file)[1] in ['.mp4','.rmvb','.avi']:
  7.             vedioList.append(os.getcwd() + os.sep + each_file +os.linesep)

  8.         if os.path.isfile(each_file):
  9.             vedio_file(each_file)
  10.             os.chdir(os.pardir)

  11. start_file = input('请输入待查找的初始目录:')
  12. vedioList = []

  13. vedio_file(start_file)

  14. f = open(os.getcwd() + os.sep + 'vedioList.txt','w')
  15. f.writelines(vedioList)
  16. f.close()
复制代码

这是我自己写的,为啥不对啊

我运行小甲鱼的程序也是有问题。。。

我写的程序

我写的程序

小甲鱼的程序

小甲鱼的程序
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-3-8 10:47:16 | 显示全部楼层
import os

def vedio_file(start_file):
    vedioList = []
    os.chdir(start_file)
    all_file = os.listdir(os.getcwd())
    for each_file in all_file:
        if os.path.splitext(each_file)[1] in ['.mp4','.rmvb','.avi']:
            vedioList.append(os.getcwd() + os.sep + each_file +os.linesep)

    return vedioList
start_file = input('请输入待查找的初始目录:')


vedioList = vedio_file(start_file)

f = open(os.getcwd() + os.sep + 'vedioList.txt','w')
f.writelines(vedioList)
f.close()

代码帮你改过了,去除了递归文件夹的代码。
原函数几个问题:
1、原函数没有返回值。
2、os.curdir是动作不是路径
3、不要自己递归查找文件夹,可以用os.walk。

评分

参与人数 1荣誉 +20 鱼币 +20 贡献 +1 收起 理由
hldh214 + 20 + 20 + 1 热爱鱼C^_^

查看全部评分

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-7 11:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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