Python Walk Calendar Catalog

Source: Internet
Author: User

Import OS
Import Fnmatch


def is_file_match (filename, patterns):
For pattern in patterns:
If Fnmatch.fnmatch (filename, pattern):
Return True
Return False


def find_specific_files (Root, patterns=[' * '], exclude_dir=[]):
For Root, Dirnames, filenames in Os.walk (root):
For filename in filenames:
If Is_file_match (filename, patterns):
Yield Os.path.join (root, filename):
For D in Exclude_dir:
If d in Dirnames:
Dirnames.remove (d)

#查找目录下所有文件
For item in Find_specific_files ('. ')
Print (item)

#查找目录下所有图片
patterns = [' *.jpg ', ' *.jpeg ', ' *.png ', ' *.tif ', ' *.tiff ']
For item in Find_specific_files ('. ', patterns)
Print (item)

#查找目录树中, except the Dir2 directory for all pictures in other directories
patterns1 = [' *.jpg ', ' *.jpeg ', ' *.png ', ' *.tif ', ' *.tiff ']
Exclude_dirs = [' Dir2 ']
For item in Find_specific_files ('. ', Patterns1,exclude_dirs):
Print (item)

#查找目录下最大的十个文件, Find_specific_files has been able to find all the files in a directory, as long as the file size and sorted by size, output the largest 10 files can be
Files = {name:os.path.getsize (name) for name in Find_specific_files ('. ')}
result = sorted (Files.items (), Key=lambda d:d[1],reverse=true) [: 10]
For I in Enumerate (result,1):
Print (i,t[0],t[1])

Python Walk Calendar Catalog

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.