NiceLabel Python 2.7 目录搜索包含多个或两个字符串的文件名

问题描述

  • 我有一个网络路径,我的所有标签模板都位于其中 (\\server\some\path)
  • NiceLabel 具有对 PartNumber 和 Type 进行 sql 查找的变量
  • 需要一个 Python (2.7) 脚本来搜索文件名并返回 PATH,其中 FileName CONTAINS PartNumber AND Type
  • 这篇文章帮助我找到了 1 个字符串所需的内容,但有没有办法搜索 2 个字符串 (Link)
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 问题!!!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)