Example file sharing for recursive records specifying suffix names in python

Source: Internet
Author: User

Only three global variables need to be changed to call directly:

# Coding=utf-8import os# Three global variables: Root_folder = './root '   # will be recursively folder root save_txt = './paths.txt ' # Record path of document Suffix_name = '. JPG '     # suffix # recursive record the absolute path of the file specifying suffix name def record (folder, save_txt):    save_file = open (Save_txt, ' a ') for    name in Os.listdir (folder):        if Os.path.isdir (os.path.join (folder, name)):            record (Os.path.join (folder, name), Save_ TXT)        elif name.endswith (suffix_name):            save_file.write (' {}\n '. Format (os.path.join (folder, name)))    Save_file.close () def main ():    # If save_txt already exists, delete    try:        os.remove (save_txt)    except OSError:        Pass    # begins the recursive recording record    (Os.path.abspath (Root_folder), save_txt) if __name__ = = ' __main__ ':    Main ()

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.