Python Glob Read all folders or file methods under path

Source: Internet
Author: User

    in Python, the Glob module is the
    The matching rules in the Unix shell need to be used in the found condition:
       *    :   matches all
       ?    :   matches one character
       *.*  :   matches such as: [Hello.txt,cat.xls,xxx234s.doc]
       ?. *  :   matches such as: [1.txt,h.py]
       ?. GIF:   matches such as: [X.gif,2.gif]

If there is no match, Glob.glob (path) will return an empty list:[]

Import Glob  def get_all ():      "' Get directory [F:\\wfpdm\\my_proc_data_zxtz] All files below '      return Glob.glob (' f:\\ Wfpdm\\my_proc_data_zxtz\\*.* ')  def get_my_file ():      "' Get directory [F:\\wfpdm\\my_proc_data_zxtz] The following file name is a 4-character file "'      return Glob.glob (' f:\\wfpdm\\my_proc_data_zxtz\\????. TXT ') def get_batch_file ():      "" Gets the directory [F:\\wfpdm\\my_proc_data_zxtz] under the file "'. txt\ ' with the extension '" '      return Glob.glob ( ' F:\\wfpdm\\my_proc_data_zxtz\\*.txt ')  def Main ():      print (' Get directory [F:\\wfpdm\\my_proc_data_zxtz] All files below: ')    tem_files = Get_all ()      print (tem_files)      print (' Get directory [F:\\wfpdm\\my_proc_data_zxtz] file named 4 characters: ')      tem_files = get_my_file ()      print (tem_files)      print (' Get directory [f:\\wfpdm\\my_proc_data_zxtz] under extension '. txt ' \ ' file: ')      tem_files = Get_batch_file ()      print (tem_files)  if __name__ = = ' __main__ ':      Main ()  


Alternatively, you can use the Join method to achieve file acquisition.

From Os.path import exists, Isdir, basename, join, splitextfrom glob import globextensions = ['. Zxtz ']def get_categories (d Atasetpath):    "Get all categories, folder name" '    cat_paths = [files for files in                 glob (Datasetpath + "/*")                  if Isdir ( files)]    cat_paths.sort ()    cats = [BaseName (Cat_path) for Cat_path in Cat_paths]    return catsdef get_files ( Path, extensions=extensions):    "returns all video filenames under the path path of the classpath, list '    all_files = []    all_files.extend ([Join] ( Path, basename (fname)),                    fname in Glob (path + "/*")                    if Splitext (fname) [1] in extensions])    return All_ Files



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python Glob Read all folders or file methods under path

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.