{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e3d91216-d411-4039-a914-3d5e23cb5335", "metadata": { "ExecutionIndicator": { "show": true }, "execution": { "iopub.execute_input": "2024-11-21T10:25:21.652021Z", "iopub.status.busy": "2024-11-21T10:25:21.651701Z", "iopub.status.idle": "2024-11-21T10:25:21.931775Z", "shell.execute_reply": "2024-11-21T10:25:21.931295Z", "shell.execute_reply.started": "2024-11-21T10:25:21.652000Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['/mnt/workspace/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts/py/play_sound.py']\n" ] } ], "source": [ "import os\n", "\n", "def find_files_with_string(directory, search_string, file_extension=None):\n", " matching_files = []\n", " # 遍历目录下的所有文件和子目录\n", " for root, dirs, files in os.walk(directory):\n", " for file in files:\n", " # 如果指定了文件格式,检查文件是否匹配\n", " if file_extension and not file.endswith(file_extension):\n", " continue\n", " \n", " file_path = os.path.join(root, file)\n", " # 逐个文件打开并检查是否包含特定字符\n", " try:\n", " with open(file_path, 'r', encoding='utf-8') as f:\n", " if search_string in f.read():\n", " matching_files.append(file_path)\n", " except (UnicodeDecodeError, FileNotFoundError):\n", " # 如果文件不能被读取或者文件不存在,跳过\n", " continue\n", " return matching_files\n", "\n", "# 示例用法\n", "directory = '/mnt/workspace'\n", "search_string = 'notify.mp3'\n", "file_extension = '.py' # 仅搜索特定格式的文件\n", "matching_files = find_files_with_string(directory, search_string, file_extension)\n", "print(matching_files)" ] }, { "cell_type": "code", "execution_count": 2, "id": "995ae9bc-5282-4403-aa6d-50839485e6b0", "metadata": { "execution": { "iopub.execute_input": "2024-11-21T10:30:56.497870Z", "iopub.status.busy": "2024-11-21T10:30:56.497554Z", "iopub.status.idle": "2024-11-21T10:30:56.505407Z", "shell.execute_reply": "2024-11-21T10:30:56.504867Z", "shell.execute_reply.started": "2024-11-21T10:30:56.497847Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "找到文件: /mnt/workspace/ComfyUI/custom_nodes/ComfyUI-Custom-Scripts/web/js/assets/notify.mp3\n" ] } ], "source": [ "import os\n", "\n", "def find_file(target_file, search_path):\n", " \"\"\"\n", " 遍历目录寻找目标文件,返回文件的完整路径。\n", " :param target_file: 要寻找的文件名\n", " :param search_path: 搜索的起始目录路径\n", " :return: 文件的完整路径,未找到返回 None\n", " \"\"\"\n", " for root, dirs, files in os.walk(search_path):\n", " if target_file in files:\n", " return os.path.join(root, target_file)\n", " return None\n", "\n", "# 示例用法\n", "if __name__ == \"__main__\":\n", " search_dir = \"/mnt/workspace/ComfyUI\" # 替换为实际的目录路径\n", " file_name = \"notify.mp3\" # 替换为你要寻找的文件名\n", "\n", " result = find_file(file_name, search_dir)\n", " if result:\n", " print(f\"找到文件: {result}\")\n", " else:\n", " print(\"未找到目标文件\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }