您当前的位置: 首页 >  Python

你是否经历过电脑文件怎么也找不到的时候?教你用Python一招解决!让电脑秘密无处可藏!

发布时间:2021-07-10 21:49:27 ,浏览量:8

在日常生活工作中,我们很多时候都是凭着记忆去找文件,要是文件突然不见了,就只能用电脑自带的搜索工具去找,但是Python也能帮助我们找文件,你知道吗?

于是老夫心血来潮用Python写了个文件搜索工具,感觉还是比电脑本身的方便一些。

先给大家安排一波福利…滑稽

大长腿

开发环境:

这是要用到的软件,大家没有的话,可以加这个群,本文的完整源码,相关视频教程等等都可以获取到 点我获取

1. 解释器: Python 3.6.5 | Anaconda, Inc.
2. 编辑器: pycharm 专业版

具体怎么实现的我就不一一去说了,大家要是只要代码的话,我会把代码放在下面,如果想学好,自己实现怎么做到的,可以看我这个教程,一步一步的手把手教大家实现点我看视频学习

部分代码:

import tkinter as tk from tkinter import messagebox, filedialog import os

root = tk.Tk() root.title('电脑文件搜索工具') root.geometry('600x300') # 搜索内容显示的组件 search_frame = tk.Frame(root) search_frame.pack() # Label 文本框 # pad padding 边框 tk.Label(search_frame, text='关键字:').pack(side=tk.LEFT, padx=10, pady=10) key_entry = tk.Entry(search_frame) key_entry.pack(side=tk.LEFT, padx=10, pady=10) tk.Label(search_frame, text='文件类型:').pack(side=tk.LEFT, padx=10, pady=10) type_entry = tk.Entry(search_frame) type_entry.pack(side=tk.LEFT, padx=10, pady=10) submit = tk.Button(search_frame, text='搜索') submit.pack(side=tk.LEFT, padx=10, pady=10) # 一个程序员的能力与他敲过的代码是成正比的,如果想成为大牛 # ctrl + 鼠标左键 查看源码 list_box = tk.Listbox(root) list_box.pack(fill=tk.BOTH, expand=True, side=tk.LEFT) def search(): print('按钮被点击了') file_type = type_entry.get() key = key_entry.get() if not key: messagebox.showinfo('出错了', '请输入关键字再搜索') return if not file_type: messagebox.showinfo('出错了', '请输入文件类型再搜索') return print('关键字:', key) print('文件类型:', file_type) # 当都输入了内容的时候,在进行操作, # 调用 Windows 的文件管理系统,获取指定的目录 fn = filedialog.askdirectory() list_box.delete(0, tk.END) print(fn) # 目录,目录的子目录,目录的文件 for root_path, dirs, files in os.walk(fn): # print(root_path, dirs, files) for file in files: file_path = root_path + '/' + file # 然后过滤指定的文件类型 if file_path.endswith(file_type): print(file_path) # 从指定文件里面查找关键字 content = open(file_path, mode='r', encoding='utf-8-sig').read() if key in content: # 将找到内容的文件路径显示到列表盒子里面 list_box.insert(tk.END, file_path) 
submit.config(command=search) """添加滚动栏""" sb = tk.Scrollbar(root) sb.pack(side=tk.LEFT, fill=tk.Y) # 将 sb 绑定到 list_box sb.config(command=list_box.yview) list_box.config(yscrollcommand=sb.set) """添加列表元素点击事件""" 

完整代码记得加上面的群获取 在这里插入图片描述

关注
打赏
1688896170
查看更多评论

暂无认证

  • 8浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.1211s