FileSystemLocal 类是一些os 和os.path 方法的简单接口。例如,FileSystemLocal 的listdir() 方法只是对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 的任何方法的参数以/ 开头,则将其视为绝对路径并忽略提供的默认目录(这是使用os.join 的效果)。李>