python - NiceLabel Python 2.7 目录搜索包含多个(或两个)字符串的文件名
问题描述
- 我有一个所有标签模板所在的网络路径(\\server\some\path)
- NiceLabel 具有对 PartNumber 和 Type 进行 SQL 查找的变量
- 需要一个 Python (2.7) 脚本来搜索文件名并返回 PATH 其中 FileName CONTAINS PartNumber AND Type
- 这篇文章帮助我找到了 1 个字符串所需的内容,但是有没有办法搜索 2 个字符串(链接)
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 问题!!!
解决方案
推荐阅读
- docker - Portainer docker容器反向代理问题
- html - css/html 适用于本地文件,而不适用于 jsfiddle 或 codepen
- xcode - 使用 prepareForSegue 在表视图之间传递索引不起作用
- properties - 为什么 hive-site.xml 中的属性没有在 hive 中获取?
- javascript - 引导弹出框内的 onclick 事件
- python - 尝试通过用户输入标记图形中的点
- postgresql - 在 OpenShift 中使用 postgresql-persistent 模板创建 postgresql 部署
- activerecord - 在 Yii 2 中处理 N+1 个查询
- nginx - 将现有 Nginx 安装与 GitLab 一起使用不起作用
- content-security-policy - 为什么在严格的 CSP 下“script.text = ...”不被认为是不安全的?