吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1009|回复: 14
收起左侧

[已解决] python 怎么获取文件夹下所有指定类型文件

[复制链接]
plaodj 发表于 2022-10-14 20:30
本帖最后由 plaodj 于 2022-10-14 20:45 编辑


假设 文件夹是 D:\work

在work文件夹里有 文件 与  子文件夹

子文件夹里面 又有 文件 与子文件夹

。。。

现在想获取work目录里面  所有  .html文件(包括子文件里面的)


要怎样写这个python   特来求助  网上找过了 没找到

花时间尝试 自己解决了 谢谢

[Python] 纯文本查看 复制代码
def getFilst(path, filetype):
    filelist = []
    for root, subDirs, files in os.walk(path):
        for fileName in files:
            if fileName.endswith(filetype):
                filelist.append(os.path.join(root, fileName))
    return filelist

免费评分

参与人数 2吾爱币 +2 热心值 +1 收起 理由
我的睡公主 + 1 + 1 我很赞同!
zhaoqingdz + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

mzbqhbc 发表于 2022-10-14 20:47
用 glob
老M 发表于 2022-10-14 21:57
archer0258 发表于 2022-10-14 23:21
[Python] 纯文本查看 复制代码
def getFilst(path, filetype):
    filelist = []
    for root, subDirs, files in os.walk(path):
        for fileName in files:
            if os.path.splitext(fileName)[-1] == filetype:
                filelist.append(os.path.join(root, fileName))
    return filelist
头像被屏蔽
tl;dr 发表于 2022-10-15 07:02
提示: 作者被禁止或删除 内容自动屏蔽
bj9ye666 发表于 2022-10-15 07:48
一起学习习诶西学习一下
吾更爱你 发表于 2022-10-15 08:19
archer0258 发表于 2022-10-14 23:21
[mw_shl_code=python,true]def getFilst(path, filetype):
    filelist = []
    for root, subDirs, fi ...

借楼请教下 如果一个A文件夹里有无数子文件夹 子文件夹里层级不一定

我想用python 将A文件夹里的所有文件都移动到A文件夹里 怎么弄呢

换句话说就是比如电影文件夹 里面有2011 2012 2013文件夹 里面还有电影 我想把这些电影都移动到电影文件夹里 中间没有文件夹了
木羊羽 发表于 2022-10-15 08:41
吾更爱你 发表于 2022-10-15 08:19
借楼请教下 如果一个A文件夹里有无数子文件夹 子文件夹里层级不一定

我想用python 将A文件夹里的所有 ...

[Python] 纯文本查看 复制代码
# Time: 2019/10/5 15:27
import os
import shutil


def copy_file(source_path, target_path):
    try:
        os.mkdir(target_path)
    except FileExistsError:
        print('目标文件夹已存在')
    count = 1
    for i in os.walk(source_path):
        if 'Video_Export' in i[0]:
            video_path = os.path.join(i[0], i[2][0])
            shutil.copy(video_path, target_path)
            print('成功复制第【%d】视频' % count)
            count += 1
    print('文件复制完成')


def move_file(source_path, target_path):
    try:
        os.mkdir(target_path)
    except FileExistsError:
        print('目标文件夹已存在')
    count = 1
    for i in os.walk(source_path):
        if 'Video_Export' in i[0]:
            video_path = os.path.join(i[0], i[2][0])
            shutil.move(video_path, target_path)
            print('成功移动第【%d】视频' % count)
            count += 1
    print('文件移动完成')


def move_file_one(source_path):
        count = 1
        for i in os.walk(source_path):
            # print(start)
            try:
                if '新建文件夹' == os.path.split(i[0])[1]:
                    shutil.rmtree(i[0])
                    continue
                if 'Video_Export' in i[1][0]:
                    for child in i[2]:
                        del_file = os.path.join(i[0], child)
                        os.remove(del_file)
            except IndexError:
                pass

        print('删除文件(夹)完成')

        for i in os.walk(source_path):
            if 'Video_Export' in i[0]:
                video_path = os.path.join(i[0], i[2][0])
                target_path = os.path.split(i[0])[0]
                # print(target_path)
                shutil.move(video_path, target_path)
                print('成功移动第【%d】视频' % count)
                count += 1

        for i in os.walk(source_path):
            if 'Video_Export' in i[0]:
                # print(start[0])
                shutil.rmtree(i[0])

        print('*****文件操作完成*****')


if __name__ == '__main__':
    # source_path = r"D:\各种文件\Actime\2020-1-22\复制\建材化学分析李宛余已翻11文本加视频\6 18李宛余已翻11"
    # target_path = r"D:\各种文件\Actime\2020-1-22\复制\建材化学分析李宛余已翻11文本加视频"
    # move_file(source_path, target_path)
    # # copy_file(source_path, target_path)

    source_path = r"D:\Impurity\Actime\12-1\12-1已完成"
    move_file_one(source_path)  # 移动到上一层目录

    

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
吾更爱你 + 1 + 1 谢谢 我学习下

查看全部评分

chinamail 发表于 2022-10-15 10:17
木羊羽 发表于 2022-10-15 08:41
[mw_shl_code=python,true]# Time: 2019/10/5 15:27
import os
import shutil

写的这么麻烦干嘛, 直接文件夹下的所有File找出来MOVE下就可以了
马了顶大 发表于 2022-10-15 11:14
吾更爱你 发表于 2022-10-15 08:19
借楼请教下 如果一个A文件夹里有无数子文件夹 子文件夹里层级不一定

我想用python 将A文件夹里的所有 ...

你这种需求用批处理解决比较好
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-17 08:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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