微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。