鱼C论坛

 找回密码
 立即注册
查看: 61|回复: 2

求问

[复制链接]
发表于 前天 23:12 | 显示全部楼层 |阅读模式

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

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

x
import os
a=input('请输入待查找的初始目录:')
def find_files(n):
    for each_dir in os.listdir(n):
        if os.path.isfile(each_dir):
            return
            
        elif os.path.isdir(each_dir):
            eachdir=each_dir
            
            find_files(eachdir)
            
if os.path.isdir(a):
   
    for each in os.listdir(a):
        if os.path.isdir(each):
            find_files(each)

        elif os.path.isfile(each):
            if os.path.splitext(each)[1]=='.mp4' or os.path.splitext(each)[1]=='.rvmb' or os.path.splitext(each)[1]=='.avi':
                file=open('VedoiList.txt','a')
                file.write(str(each)+'\n')

else:
    print('请输入目录形式')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 昨天 20:19 | 显示全部楼层
鱼油的代码存在几个关键问题,下面给出修正方案:


主要问题分析
  • 路径拼接错误:未使用os.path.join导致路径判断失效
  • 递归逻辑错误:遇到第一个文件就return终止遍历
  • 扩展名判断错误:os.path.splitext返回的是元组需取[1]
  • 未处理隐藏目录:直接遍历可能遇到权限问题


修正代码
[code=python]
import os

def find_files(root_dir):
    for file_name in os.listdir(root_dir):
        full_path = os.path.join(root_dir, file_name)  # 关键!拼接完整路径
        
        if os.path.isfile(full_path):
            ext = os.path.splitext(file_name)[1].lower()  # 取扩展名并转小写
            if ext in ('.mp4', '.rmvb', '.avi'):
                with open('VideoList.txt', 'a', encoding='utf-8') as f:
                    f.write(full_path + '\n')
                    
        elif os.path.isdir(full_path):
            find_files(full_path)  # 递归时传递完整路径

if __name__ == '__main__':
    target_dir = input('请输入待查找的初始目录:')
    if os.path.isdir(target_dir):
        find_files(target_dir)
        print("查找完成,结果已保存到VideoList.txt")
    else:
        print("错误:输入的路径不是有效目录")
[/code]

改进说明
  • 使用os.path.join保证路径正确性
  • 递归时传递完整路径参数
  • 将扩展名转为小写统一判断
  • 使用with open保证文件安全关闭
  • 增加编码参数避免乱码问题
  • 支持.rmvb扩展名(原代码拼写错误.rvmb)


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 22:48 | 显示全部楼层
本帖最后由 jackz007 于 2025-4-2 23:10 编辑
import os

def find_files(n):
    for each in os . listdir(n) :
        x = os . path . join(n , each)                      # 此句是关键,x 为 each 的绝对路径,非常有用
        if os . path . isfile(x) :                          # 在不改变当前路径的前提下,判定 each 是否为文件必须使用其绝对路径
            ext = os . path . splitext(each)[1] . lower()
            if ext in ('.mp4' , '.rmvb' , '.rm' , '.avi' , '.mkv' , '.ts') :
                with open('VedoiList.txt','a') as fp :
                    fp . write(x + '\n')
        elif os . path . isdir(x) :                         # 在不改变当前路径的前提下,判定 each 是否为子目录也必须使用其绝对路径
            find_files(x)                                   # 在不改变当前路径的前提下,find_files() 的输入参数必须是 each 的绝对路径

while True :
    a = input('请输入待查找的初始目录 [Exit]:') . strip()
    if a :
        if os . path . exists(a) and os . path . isdir(a) :
            if os . path . exists('VedoiList.txt') :        # 由于按追加方式记录查找结果,为了避免多次记录累加,必须事先删除记录文件
                os . remove('VedoiList.txt')                # 由于按追加方式记录查找结果,为了避免多次记录累加,必须事先删除记录文件
            find_files(a)
            break
        else :
            print('请输入一个目录\n')    
    else :
        break
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-3 07:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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