搜索并打开文件程序

2024-04-19 08:59:54 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是场景: 一个文件夹下有1000个文件。LP100-LP1000 我想创建一个程序,搜索名称,打开它并循环。 假设我想打开LP176。我希望Python找到并打开文件夹下的文件,打开pdf文件,然后循环相同的程序,为下一次搜索做好准备。有什么办法让我开始吗?你知道吗


Tags: 文件程序文件夹名称pdf场景办法lp100
2条回答

以下问题可以为您提供如何打开PDF文件的提示:

Open document with default application in Python

要永远循环,请执行以下操作:

while True:
    # your code here

有一种简单的方法可以使用Tkinter来做这类事情。下面的例子将是一个很好的起点。 您必须在窗口中单击才能开始键入。。。你知道吗

它在directory中查找与搜索字符串匹配的所有文件,搜索字符串会根据您的按键不断更新。按deletebackspace将从搜索字符串中删除最后一个字符。 按spacebar(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()

相关问题 更多 >