Python 搜索并打开一个文件程序
这是一个场景: 一个文件夹下有1000个文件。LP100-LP1000 我想创建一个程序,搜索名称,打开它并循环。Python 搜索并打开一个文件程序,python,function,search,Python,Function,Search,这是一个场景: 一个文件夹下有1000个文件。LP100-LP1000 我想创建一个程序,搜索名称,打开它并循环。 假设我想打开LP176。我希望Python找到并打开文件夹下的文件,打开pdf文件,然后循环相同的程序,为下一次搜索做好准备。有什么想法可以让我开始吗?以下问题可以为您提供如何打开PDF文件的提示: 要永远循环,请执行以下操作: while True: # your code here 使用Tkinter有一种简单的方法来做这类事情。下面的例子将是一个很好的起点。 您必
假设我想打开LP176。我希望Python找到并打开文件夹下的文件,打开pdf文件,然后循环相同的程序,为下一次搜索做好准备。有什么想法可以让我开始吗?以下问题可以为您提供如何打开PDF文件的提示: 要永远循环,请执行以下操作:
while True:
# your code here
使用
Tkinter
有一种简单的方法来做这类事情。下面的例子将是一个很好的起点。
您必须在窗口中单击才能开始键入
它在目录
中查找与搜索字符串匹配的所有文件,搜索字符串会根据您的按键不断更新。按delete
或backspace
将从搜索字符串中删除最后一个字符。
按空格键(keycode==32)将打开与搜索字符串匹配的所有文件
from Tkinter import *
import os
from subprocess import Popen
root = Tk()
directory = 'C:\stack'
filenames = list(os.walk(directory))[0][2]
string = ''
def main(event):
global string
global directory
if event.keycode == 32:
for filename in filenames:
if string in filename:
Popen(filename, shell=True)
return
if event.keycode == 46 or event.keycode == 8:
string = string[:-1]
else:
string += event.char.strip()
print 'string:', string
print 'matches:', [i for i in filenames if string in i]
frame = Frame(root, width=100, height=100)
frame.bind("<Key>", main)
frame.focus_set()
frame.pack()
root.mainloop()
从Tkinter导入*
导入操作系统
从子流程导入Popen
root=Tk()
目录='C:\stack'
filenames=list(os.walk(directory))[0][2]
字符串=“”
def主(事件):
全局字符串
全局目录
如果event.keycode==32:
对于文件名中的文件名:
如果文件名中有字符串:
Popen(文件名,shell=True)
返回
如果event.keycode==46或event.keycode==8:
字符串=字符串[:-1]
其他:
string+=event.char.strip()
打印“字符串:”,字符串
打印“匹配:”,[i为文件名中的i,如果i中有字符串]
框架=框架(根,宽度=100,高度=100)
frame.bind(“,main)
frame.focus_set()
frame.pack()
root.mainloop()