继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

《Python编程快速上手》实践练习

零基础h5前端入门培训
关注TA
已关注
手记 301
粉丝 37
获赞 253

#!python3

#9.8.1

#遍历目录树,查找特定扩展名的文件(自定义)

#and把查找到的文件,copy到新文件夹

import os,shutil


file_dir=input("输入要查找的目录:")

file_dir=os.path.abspath(file_dir)

file_list=[]

if not os.path.exists(file_dir):

    print("目录不存在")

else:

    file_type=input("输入要查找文件类型的扩展名(如.pdf或.jpg):")

    file_type=file_type.lower()

    for folder,subfolders,files in os.walk(file_dir):

        for fi in files:

            if fi.lower().endswith(file_type):

                file_list.append( os.path.join(folder,fi))

#复制

destination=input("输入要存放文件的目录:")

destination=os.path.abspath(destination)

if  not  os.path.exists(destination):

    print("目录不存在")

else:

    for file in file_list:

        #未检测重复覆盖

        shutil.copy(file,destination)

print("End")


"""

#9.8.2

指定目录,遍历,查找大于100MB的,可用os.path.getsize()

些文件的abspath输出

"""

import os


ch_path=input("输入要查找的目录:")

absdir=os.path.abspath(ch_path)


for a,b,c in os.walk(absdir):

    for file in c:

        file_path=os.path.join(a,file)

        size=os.path.getsize(file_path)/1024**2

        if size>100:

            print("file is {}, {:.2f}M".format(file_path,size))


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP