鱼C论坛

 找回密码
 立即注册
查看: 1298|回复: 8

[已解决]课后题30第3题

[复制链接]
发表于 2019-10-26 10:52:17 | 显示全部楼层 |阅读模式
6鱼币
以下是自己编的程序,只是显示一个空的txt文件,不知道那里有问题!
  1. import os
  2. def search_video(start_dir):
  3.     f = open('videolist.txt','w')
  4.     os.chdir(start_dir)
  5.     for each_file in os.listdir(os.curdir):
  6.         if os.path.isfile(each_file):
  7.             a =os.path.splitext(each_file)
  8.             if a[1] in ['.mp4','.rmvb','.avi']:
  9.                 content = os.getcwd()  +each_file
  10.                 f.write(content)
  11.         
  12.         else:
  13.             search_video(each_file)
  14.             os.chdir(os.pardir)
  15.     f.close()
  16. start_dir = input('请输入待查找的初始目录:')
  17. search_video(start_dir)
  18.                
复制代码
最佳答案
2019-10-26 10:52:18
清风慕竹99 发表于 2019-10-26 16:22
重新修改了一下原程序,加了个print想看为什么写入不了txt,程序如下
不知道为什么运行后的结果是下面这 ...

def search_video(start_dir):

    else:
            search_video(each_file)

最佳答案

查看完整内容

def search_video(start_dir): else: search_video(each_file)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-26 10:52:18 | 显示全部楼层    本楼为最佳答案   
清风慕竹99 发表于 2019-10-26 16:22
重新修改了一下原程序,加了个print想看为什么写入不了txt,程序如下
不知道为什么运行后的结果是下面这 ...

def search_video(start_dir):

    else:
            search_video(each_file)

评分

参与人数 1鱼币 +5 收起 理由
清风慕竹99 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-26 11:18:47 | 显示全部楼层
  1. import os

  2. def search_video_start(start_dir, content): # 传入content列表   
  3.     os.chdir(start_dir)
  4.    
  5.     for each_file in os.listdir(os.curdir):
  6.         if os.path.isfile(each_file):
  7.             a =os.path.splitext(each_file)
  8.             if a[1] in ['.mp4','.rmvb','.avi']:
  9.                 content.append(os.getcwd()  + each_file + '\n')            
  10.         else:
  11.             search_video_start(each_file, content)
  12.             os.chdir(os.pardir)

  13. def search_video(start_dir):
  14.     content = [] # 声明个空列表
  15.     search_video_start(start_dir, content)
  16.     dirname, filename = os.path.split(os.path.abspath(__file__))
  17.     os.chdir(dirname) # 切换回脚本所在路径
  18.     with open('videolist.txt','w') as f:
  19.         f.writelines(content)
  20.         
  21. start_dir = input('请输入待查找的初始目录:')
  22. search_video(start_dir)
  23.                
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-26 14:40:36 | 显示全部楼层

dirname, filename = os.path.split(os.path.abspath(__file__))
    os.chdir(dirname) # 切换回脚本所在路径
    with open('videolist.txt','w') as f:
        f.writelines(content)
后面这几句,是不是将txt文件写入程序所在的文件夹内?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-26 15:07:23 | 显示全部楼层
清风慕竹99 发表于 2019-10-26 14:40
dirname, filename = os.path.split(os.path.abspath(__file__))
    os.chdir(dirname) # 切换回脚本所 ...

是的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-26 15:31:00 | 显示全部楼层

我还是搞不懂为什么我的原程序怎么就写不进txt文件里面
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-26 16:22:19 | 显示全部楼层

重新修改了一下原程序,加了个print想看为什么写入不了txt,程序如下
  1. import os
  2. def search_video(start_dir):
  3.     content=[]
  4.     f = open('videolist.txt','w')
  5.     os.chdir(start_dir)
  6.     for each_file in os.listdir(os.curdir):
  7.         if os.path.isfile(each_file):
  8.             a =os.path.splitext(each_file)
  9.             if a[1] in ['.mp4','.rmvb','.avi']:
  10.                 content.append(os.getcwd()  +each_file+ '\n')
  11.                 #f.write(content)
  12.         
  13.         else:
  14.             search_video(each_file)
  15.             os.chdir(os.pardir)
  16.     print(content)
  17.     f.writelines(content)
  18.     f.close()
  19. start_dir = input('请输入待查找的初始目录:')
  20. search_video(start_dir)
  21.                
复制代码

不知道为什么运行后的结果是下面这样的?
[]
[]
[]
[]
[]
[]
[]
[]
[]
['E:\\python\\video1.mp4\n', 'E:\\python\\video2.rmvb\n', 'E:\\python\\video3.avi\n']
[]
[]
[]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-27 20:18:40 | 显示全部楼层
ba21 发表于 2019-10-26 10:52
def search_video(start_dir):

    else:

大哥我好像懂了一些,但关于递归还是有一些混乱;比如这个递归向里搜索和最外面外面搜索是同步的吗?不知道你知否听明白我的表述
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-27 20:42:57 | 显示全部楼层
清风慕竹99 发表于 2019-10-27 20:18
大哥我好像懂了一些,但关于递归还是有一些混乱;比如这个递归向里搜索和最外面外面搜索是同步的吗?不知 ...

不在一个域的变量,即便同名也是各归各的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 05:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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