需要创建这种类型的对象(来自 kivy.properties & Kivy filechooser)
ObjectProperty(FileSystemLocal(), baseclass=FileSystemAbstract)
需要创建这种类型的对象(来自 kivy.properties & Kivy filechooser)
ObjectProperty(FileSystemLocal(), baseclass=FileSystemAbstract)
该类FileSystemLocal
是一些os
和os.path
方法的简单接口。例如, 的listdir()
方法FileSystemLocal
只是对 的调用os.listdir()
。所以它不是特定于任何目录,它只是特定于本地os
和os.path
. 所以,从技术上讲,答案是否定的。
也许您可以定义自己的FileSystemLocal
子类来满足您的要求。
FileSystemLocal
以下是使用特定目录的扩展示例:
from os import listdir
from os.path import (basename, join, getsize, isdir)
from sys import platform as core_platform
from kivy import Logger
from kivy.uix.filechooser import FileSystemAbstract, _have_win32file
platform = core_platform
_have_win32file = False
if platform == 'win':
# Import that module here as it's not available on non-windows machines.
try:
from win32file import FILE_ATTRIBUTE_HIDDEN, GetFileAttributesExW, \
error
_have_win32file = True
except ImportError:
Logger.error('filechooser: win32file module is missing')
Logger.error('filechooser: we cant check if a file is hidden or not')
class FileSystemLocalDir(FileSystemAbstract):
def __init__(self, **kwargs):
self.dir = kwargs.pop('dir', None)
super(FileSystemLocalDir, self).__init__()
def listdir(self, fn):
if self.dir is not None:
fn = join(self.dir, fn)
print('listdir for', fn)
return listdir(fn)
def getsize(self, fn):
if self.dir is not None:
fn = join(self.dir, fn)
return getsize(fn)
def is_hidden(self, fn):
if self.dir is not None:
fn = join(self.dir, fn)
if platform == 'win':
if not _have_win32file:
return False
try:
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
except error:
# This error can occurred when a file is already accessed by
# someone else. So don't return to True, because we have lot
# of chances to not being able to do anything with it.
Logger.exception('unable to access to <%s>' % fn)
return True
return basename(fn).startswith('.')
def is_dir(self, fn):
if self.dir is not None:
fn = join(self.dir, fn)
return isdir(fn)
这可以用作:
fsld = FileSystemLocalDir(dir='/home')
print('dir:', fsld.dir)
print('listdir .:', fsld.listdir('.'))
print('listdir freddy:', fsld.listdir('freddy')) # lists home directory of user `freddy`
print('listdir /usr:', fsld.listdir('/usr')) # this will list /usr regardless of the setting for self.dir
笔记:
FileSystemLocalDir
基于FileSystemLocal
.dir=
函数中的 设置为所有方法查询的默认目录FileSystemLocalDir
。dir=
未提供参数,FileSystemLocalDir
则 等效于FileSystemLocal
。FileSystemLocalDir
a 开头/
,则将其视为绝对路径并忽略提供的默认目录(这是使用 的效果os.join
)。