『python工作』文件搜索器

#! usr/bin/env python
# -*- coding:utf-8 -*-

"""
文件搜索器
"""

FILE_PATH = r"C:\Scrapy\WJX"

global main_file_list
main_file_list = []


import os


def find_main(file_path, word="main"):
    """
    查找 Scrapy/WJX 文件夹下的所有 main.py 文件
    :param file_path:
    :return:
    """

    global main_file_list
    all_file_name = os.listdir(file_path)
    for file_name in all_file_name:
        sub_file_path = file_path + "\\%s" % file_name
        if os.path.isdir(sub_file_path):
            os.chdir(sub_file_path)
            find_main(sub_file_path)
        else:
            # 判断文件是否符合条件
            if sub_file_path.split("\\")[-1].split(".")[0] == word and \
                            sub_file_path.split("\\")[-1].split(".")[1] == "py":
                main_file_list.append(sub_file_path)

    return main_file_list


def main(file_path):

    all_main_file_path = find_main(file_path)

    return

if __name__ == '__main__':
    main(FILE_PATH)

你可能感兴趣的:(工作)