首页 > 解决方案 > NiceLabel Python 2.7 目录搜索包含多个(或两个)字符串的文件名

问题描述

import os
import glob

# Sets the main directory
main_path = "C:\\Users\\Dawn Philip\\Documents\\documents"

# Gets a list of everything in the main directory including folders
main_directory = os.listdir(main_path)

# This list will hold all of the folders to search through, including the main folder
sub_directories = []

# Adds the main folder to to the list of folders
sub_directories.append(main_path)

# Loops through everthing in the main folder, searching for sub folders
for item in main_directory:
    # Creates the full path to each item)
    item_path = os.path.join(main_path, item)

    # Checks each item to see if it is a directory
    if os.path.isdir(item_path) == True:
        # If it is a folder it is added to the list
        sub_directories.append(item_path)

for directory in sub_directories:
    for files in glob.glob(os.path.join(directory,"*.txt")):
        f = open( files, 'r' )
        file_contents = f.read()
        if "x" in file_contents:
            print f.name

我的第一个 StackOverflow 问题!!!

标签: pythonpython-2.7glob

解决方案


推荐阅读