本文目录导读:

Python 实现
基础全盘搜索
import os
import sys
def search_files(search_term, root_path='/' if sys.platform != 'win32' else 'C:\\'):
"""全盘搜索文件"""
results = []
try:
for root, dirs, files in os.walk(root_path):
# 搜索文件名
for file in files:
if search_term.lower() in file.lower():
results.append(os.path.join(root, file))
# 搜索目录名
for dir_name in dirs:
if search_term.lower() in dir_name.lower():
results.append(os.path.join(root, dir_name))
except (PermissionError, OSError) as e:
print(f"访问被拒绝: {e}")
return results
# 使用示例
search_term = input("请输入搜索内容: ")
results = search_files(search_term)
print(f"找到 {len(results)} 个结果:")
for r in results[:20]: # 只显示前20个
print(r)
带进度的多盘搜索
import os
import threading
from pathlib import Path
class FullDiskSearch:
def __init__(self, search_term):
self.search_term = search_term.lower()
self.results = []
self.scanned = 0
def search_disk(self, disk_path):
"""搜索单个磁盘"""
try:
for root, dirs, files in os.walk(disk_path, topdown=True):
# 跳过系统目录
skip_dirs = {'windows', 'system32', '$recycle.bin',
'system volume information', 'pagefile.sys'}
dirs[:] = [d for d in dirs if d.lower() not in skip_dirs]
self.scanned += len(files) + len(dirs)
for item in dirs + files:
if self.search_term in item.lower():
self.results.append(os.path.join(root, item))
except Exception as e:
pass
def search_all_disks(self):
"""搜索所有磁盘"""
import platform
if platform.system() == "Windows":
# Windows 搜索所有盘符
for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
disk = f"{letter}:\\"
if os.path.exists(disk):
print(f"正在搜索 {disk}...")
self.search_disk(disk)
else:
# Linux/Unix 搜索根目录
print("正在搜索 / ...")
self.search_disk('/')
return self.results
# 使用
searcher = FullDiskSearch("重要文件")
results = searcher.search_all_disks()
命令行方式
Windows (PowerShell)
# 搜索所有文件 Get-ChildItem -Path C:\ -Recurse -Filter "*关键词*" -ErrorAction SilentlyContinue # 搜索特定类型 Get-ChildItem -Path D:\ -Recurse -Filter "*.pdf" -ErrorAction SilentlyContinue
Linux/Mac (Shell)
#!/bin/bash # 全盘搜索文件 find / -name "*关键词*" 2>/dev/null # 搜索文本内容 grep -r "关键词" / 2>/dev/null # 排除某些目录 find / -name "*关键词*" -not -path "*/proc/*" -not -path "*/sys/*" 2>/dev/null
高级 Python 脚本
#!/usr/bin/env python3
import os
import sys
import argparse
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
class EnhancedFileSearch:
def __init__(self):
self.results = []
self.total_files = 0
self.start_time = None
def search_directory(self, root_path, search_term, file_types=None):
"""搜索目录(支持文件类型过滤)"""
local_results = []
try:
for root, dirs, files in os.walk(root_path, topdown=True):
# 跳过系统目录
skip_dirs = {'.git', '__pycache__', 'node_modules',
'System32', 'Windows', 'AppData'}
dirs[:] = [d for d in dirs if d not in skip_dirs]
for file in files:
self.total_files += 1
# 文件类型过滤
if file_types:
ext = os.path.splitext(file)[1].lower()
if ext not in file_types:
continue
# 搜索文件名
if search_term.lower() in file.lower():
filepath = os.path.join(root, file)
local_results.append({
'path': filepath,
'size': os.path.getsize(filepath),
'modified': os.path.getmtime(filepath)
})
except (PermissionError, OSError):
pass
return local_results
def get_all_drives(self):
"""获取所有可访问的驱动器"""
if sys.platform == 'win32':
import string
drives = []
for letter in string.ascii_uppercase:
drive = f"{letter}:\\"
if os.path.exists(drive):
drives.append(drive)
return drives
else:
return ['/']
def search_all(self, search_term, file_types=None, max_workers=4):
"""并行搜索所有驱动器"""
self.start_time = time.time()
drives = self.get_all_drives()
print(f"开始全盘搜索: '{search_term}'")
print(f"扫描驱动器: {', '.join(drives)}")
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = []
for drive in drives:
future = executor.submit(
self.search_directory,
drive,
search_term,
file_types
)
futures.append(future)
for future in as_completed(futures):
self.results.extend(future.result())
elapsed = time.time() - self.start_time
return self.results
def display_results(self, max_results=50):
"""显示结果"""
print(f"\n搜索完成!共扫描 {self.total_files} 个文件")
print(f"找到 {len(self.results)} 个匹配项")
print(f"用时: {time.time() - self.start_time:.2f} 秒\n")
if not self.results:
print("未找到匹配的文件")
return
# 按修改时间排序
self.results.sort(key=lambda x: x['modified'], reverse=True)
for i, result in enumerate(self.results[:max_results], 1):
size_mb = result['size'] / (1024 * 1024)
mod_time = time.strftime(
'%Y-%m-%d %H:%M:%S',
time.localtime(result['modified'])
)
print(f"{i:3}. {result['path']}")
print(f" 大小: {size_mb:.2f} MB, 修改时间: {mod_time}")
if len(self.results) > max_results:
print(f"\n... 还有 {len(self.results) - max_results} 个结果未显示")
def main():
parser = argparse.ArgumentParser(description='全盘文件搜索工具')
parser.add_argument('search_term', help='搜索关键词')
parser.add_argument('-t', '--types', nargs='*',
help='文件类型过滤 (如 .pdf .txt .doc)')
parser.add_argument('-w', '--workers', type=int, default=4,
help='并行工作线程数')
parser.add_argument('-n', '--max-results', type=int, default=50,
help='显示最大结果数')
args = parser.parse_args()
searcher = EnhancedFileSearch()
# 格式化文件类型
file_types = None
if args.types:
file_types = [t.lower() if t.startswith('.') else f'.{t.lower()}'
for t in args.types]
# 执行搜索
results = searcher.search_all(
args.search_term,
file_types,
args.workers
)
# 显示结果
searcher.display_results(args.max_results)
if __name__ == "__main__":
main()
使用示例
# 简单搜索 python search_script.py "重要文档" # 搜索PDF文件 python search_script.py "报告" --types .pdf # 使用8个线程并行搜索 python search_script.py "代码" --workers 8 # 显示更多结果 python search_script.py "照片" --max-results 100
注意事项
- 性能影响:全盘搜索会消耗大量CPU和磁盘I/O
- 权限问题:某些系统目录可能需要管理员权限
- 缓存考虑:对于频繁搜索,建议建立索引缓存
- 安全警告:不要在生产环境随意执行全盘搜索
这些脚本可以根据你的具体需求进行修改和扩展。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播