Python: find all files in a folder

Source: Internet
Author: User
Python searches for all files in a folder. For more information, see. The code is as follows:


Def find_file_by_pattern (pattern = '. *', base = ".", circle = True ):
''''' Search for all ''' under a given folder '''
Re_file = re. compile (pattern)
If base = ".":
Base = OS. getcwd ()

Final_file_list = []
Print base
Cur_list = OS. listdir (base)
For item in cur_list:
If item = ". svn ":
Continue

Full_path = OS. path. join (base, item)
If full_path.endswith (". doc") or \
Full_path.endswith (". bmp") or \
Full_path.endswith (". wpt") or \
Full_path.endswith (". dot "):
Continue

# Print full_path
Bfile = OS. path. isfile (item)
If OS. path. isfile (full_path ):
If re_file.search (full_path ):
Final_file_list.append (full_path)
Else:
Final_file_list + = find_file_by_pattern (pattern, full_path)
Return final_file_list

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.