Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 表达方式;value.get();返回相同值的倍数_Python_Python 3.x_File - Fatal编程技术网

Python 表达方式;value.get();返回相同值的倍数

Python 表达方式;value.get();返回相同值的倍数,python,python-3.x,file,Python,Python 3.x,File,对于下面的代码,在findspec命令中,我试图只返回用户键入的文件路径和目录名。如果不存在文件,则显示“未找到文件” 这是不工作的部分: def findspec(): r1 = tar.get() rootdir = '.' for dirname, subdirlist, filelist in os.walk(rootdir, topdown=False): for file in filelist: if fnmatch.

对于下面的代码,在
findspec
命令中,我试图只返回用户键入的文件路径和目录名。如果不存在文件,则显示“未找到文件”

这是不工作的部分:

def findspec():
    r1 = tar.get()
    rootdir = '.'
    for dirname, subdirlist, filelist in os.walk(rootdir, topdown=False):
        for file in filelist:
            if fnmatch.fnmatch(file, tar.get()):
                list1.insert(END, dirname + "--->" + str(r1))
            else:
                list1.insert(END, "File not found")
它应该返回以下内容:

User/%User%/directory ---> config.py
User/%User%/directory/static ---> config.py
而是返回以下内容:

File not FOund
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
User/%User%/directory ---> config.py
File not Found
File not Found
User/%User%/directory/static ---> config.py
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
File not Found
源代码如下:

import os
import fnmatch
import tkinter
from tkinter import *
from difflib import get_close_matches

def searchdir():
    rootdir = '.'
    for dirname, subdirlist, filelist in os.walk(rootdir, topdown=False):
        for fname in filelist:
            list1.insert(END, dirname +  '\t%s' % fname)

def findspec():
    r1 = tar.get()
    rootdir = '.'
    for dirname, subdirlist, filelist in os.walk(rootdir, topdown=False):
        for file in filelist:
            if fnmatch.fnmatch(file, tar.get()):
                list1.insert(END, dirname + "--->" + str(r1))
            else:
                list1.insert(END, "File not found")

w=Tk()

b1 = Button(w, text="Search", command=findspec)
b1.grid(row=0, column=0, columnspan=2)

tar=StringVar()
e1 = Entry(w, textvariable=tar)
e1.grid(row=0, column=1, columnspan=4)

b2 = Button(w, text="Start", command=searchdir)
b2.grid(row=1, column=0, columnspan=2)

#listbox
list1=Listbox(w, height=17, width=75, selectmode=EXTENDED)
list1.grid(row=2, column=0, rowspan=7, columnspan=4, sticky=(N, S, E), pady=5, padx=5)

sb1=Scrollbar(w)
sb1.grid(row=2, column=4, rowspan=7, sticky='nsw', pady=5)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

sb2=Scrollbar(w, orient=HORIZONTAL)
sb2.grid(row=10, column=0, columnspan=4, sticky='esw', padx=5)
list1.configure(xscrollcommand=sb2.set)
sb2.configure(command=list1.xview)

list1.bind('<<ListboxSelect>>')

if __name__=='__main__':
    w.mainloop()
导入操作系统
导入fnmatch
进口tkinter
从tkinter进口*
从difflib导入获取\u关闭\u匹配
def searchdir():
rootdir='。'
对于os.walk(rootdir,top-down=False)中的dirname、subdirlist和filelist:
对于文件列表中的fname:
列表1.insert(结束,目录名+'\t%s“%fname”)
def findspec():
r1=tar.get()
rootdir='。'
对于os.walk(rootdir,top-down=False)中的dirname、subdirlist和filelist:
对于文件列表中的文件:
如果fnmatch.fnmatch(文件tar.get()):
列表1.插入(结束,目录名+“-->”+str(r1))
其他:
列表1.插入(结束,“未找到文件”)
w=Tk()
b1=按钮(w,text=“Search”,command=findspec)
b1.网格(行=0,列=0,列span=2)
tar=StringVar()
e1=条目(w,textvariable=tar)
e1.网格(行=0,列=1,列跨度=4)
b2=按钮(w,text=“Start”,command=searchdir)
b2.网格(行=1,列=0,列span=2)
#列表框
列表1=列表框(w,高度=17,宽度=75,选择模式=扩展)
列表1.网格(行=2,列=0,行span=7,列span=4,粘性=(N,S,E),pady=5,padx=5)
sb1=滚动条(w)
sb1.网格(行=2,列=4,行span=7,新南威尔士州,帕迪=5)
list1.configure(yscrollcommand=sb1.set)
sb1.configure(命令=list1.yview)
sb2=滚动条(w,方向=水平)
sb2.网格(行=10,列=0,列span=4,粘滞=esw',padx=5)
配置(xscrollcommand=sb2.set)
sb2.configure(命令=list1.xview)
列表1.bind(“”)
如果“名称”=“\uuuuuuuu主要”:
w、 mainloop()
我是做错了什么,只是错过了什么,还是别的?
非常感谢您的帮助。

对于文件列表中的文件:
将对
文件列表中的每个文件执行
if/else
检查。但是,似乎不是每次
fnmatch.fnmatch()
失败时都要执行
else
子句,而是只在搜索已耗尽且没有结果添加到列表时执行,因此当
len(filelist)<1
时:

if fnmatch.fnmatch(file, r1):
    list1.insert(END, dirname + "--->" + str(r1))
elif list1.size() < 1:
    list1.insert(END, "File not found")
如果fnmatch.fnmatch(文件,r1):
列表1.插入(结束,目录名+“-->”+str(r1))
elif list1.size()<1:
列表1.插入(结束,“未找到文件”)

这一行将给您一个语法错误:
r1=tar.get())
。如果应该,则不是。它们只是意味着在关闭
get()
方法调用时有一个拼写错误(额外的参数)
)。它可能不存在于您正在运行的代码中,但在您将代码粘贴到问题中时可能意外包含了它。啊,我现在明白了,谢谢。它需要在问题中修复。第一部分很有魅力,谢谢!然而,第二部分并非如此。它不显示“未找到文件”值。感谢您的反馈。抱歉,我误解了
list1
是一个Tkinter列表框。我已将代码更新为使用
size()
方法。