This article describes using Python to scan files in a specified directory, or a function that matches a specified suffix and prefix. The steps are as follows:
If you want to scan files in a specified directory, including subdirectories, you need to call Scan_files ("/export/home/test/")
If you want to scan a file (such as a jar package) for a specific suffix in a specified directory, including subdirectories, call Scan_files ("/export/home/test/", postfix= ". Jar")
If you want to scan files (such as test_xxx.py) for a specific prefix in a specified directory, including subdirectories, call Scan_files ("/export/home/test/", postfix= "Test_")
The specific implementation code is as follows:
#!/usr/bin/env python
#coding =utf-8
import OS
def scan_files (directory,prefix=none,postfix=none):
files_list=[]
for root, sub_dirs, files in Os.walk (directory): For
special_file in Files:
if postfix:
if Special_file.endswith (postfix):
files_list.append (Os.path.join (root,special_file))
elif Prefix:
if Special_file.startswith (prefix):
files_list.append (Os.path.join (root,special_file))
else:
files_list.append (Os.path.join (root,special_file)) return
files_list