"Python Programming Quick Start" 9.8.1 practical exercises

Source: Internet
Author: User

#!python3#9.8.1#遍历目录树,查找特定扩展名的文件(自定义)#and把查找到的文件,copy到新文件夹import os,shutilfile_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 osch_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 Programming Quick Start" 9.8.1 practical exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.