找到你要的答案

Q:Python Kivy: Search directory for filetype

Q:Python基维:对文件搜索目录

There are many ways, to search a dir for containing a string, that's not really my question. But is there something built in for Python Kivy, that allows automatically searching for files (*.mp3) in a directory with subdirs, or do I have to create one on my own? If I have to do so, how do I get all the files and subdirs in a directory?

Thank you :)

Finally I decided coding the needed function on my own:

import os
def listfiles(path):
     files = []
     for base, directory, filename in os.walk(path):
          for i in range(len(filename)):
               files.append(base+"/"+filename[i])
     return files
print(listfiles("/path/path/"))

Checking files for extensions should be easy enaugh :)

Unfortunately the process might take very long for bigger directories, so I'm still looking for a different solution.

There are many ways, to search a dir for containing a string, that's not really my question. But is there something built in for Python Kivy, that allows automatically searching for files (*.mp3) in a directory with subdirs, or do I have to create one on my own? If I have to do so, how do I get all the files and subdirs in a directory?

谢谢:)

最后我决定自己编写所需要的函数:

import os
def listfiles(path):
     files = []
     for base, directory, filename in os.walk(path):
          for i in range(len(filename)):
               files.append(base+"/"+filename[i])
     return files
print(listfiles("/path/path/"))

检查文件的扩展名应该很容易不够的:)

不幸的是,这个过程可能需要更长的时间为更大的目录,所以我仍然在寻找一个不同的解决方案。

answer1: 回答1:

Check out Kivy kivy.uix.filechooser FileChooserController and its method files

The list of files in the directory specified by path after applying the filters.

files is a read-only ListProperty.

@edit

Here's what I also found in Kivy docs and this one seems even nicer:

from kivy.uix.filechooser import FileSystemLocal
file_system = FileSystemLocal()
file_system.listdir('/path/to/dir')   # this returns a list of files in dir

Instead of FileSystemLocal you can also use FileSystemAbstract if you are not going to browse only local files.

看看Kivy kivy.uix.filechooser FileChooserController及其方法的文件

应用筛选器后由路径指定的目录中的文件列表。

文件是只读的listproperty。

@编辑

在这里我也发现基维文档和这个似乎更好:

from kivy.uix.filechooser import FileSystemLocal
file_system = FileSystemLocal()
file_system.listdir('/path/to/dir')   # this returns a list of files in dir

而filesystemlocal你也可以不去浏览本地文件使用filesystemabstract。

answer2: 回答2:
files = [] # list with files in directory
suff = ('.mp3', '.wav')
for i in files:
    if i.endswith(suff):
        print files

Suffixes needs to be tuples for this to work.

files = [] # list with files in directory
suff = ('.mp3', '.wav')
for i in files:
    if i.endswith(suff):
        print files

后缀需要元组这个工作。

python  kivy  file-search