首页 > 编程语言 > 详细

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

时间:2018-03-12 18:30:15      阅读:181      评论:0      收藏:0      [点我收藏+]
#!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))

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

原文:http://blog.51cto.com/2884868/2085593

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!