【问题标题】:Can I create a filesystem object of a specific directory?我可以创建特定目录的文件系统对象吗?
【发布时间】:2020-07-26 04:10:31
【问题描述】:

需要创建这种类型的对象(来自 kivy.properties & Kivy filechooser)

ObjectProperty(FileSystemLocal(), baseclass=FileSystemAbstract)

【问题讨论】:

  • 我认为最好的选择是导入 os 并使用 bash 命令
  • 不确定我是否了解如何使用它来满足此需求,您能帮忙吗?

标签: python kivy filesystemobject object-property


【解决方案1】:

FileSystemLocal 类是一些osos.path 方法的简单接口。例如,FileSystemLocallistdir() 方法只是对os.listdir() 的调用。所以它并不特定于任何目录,它只是特定于本地的osos.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 的效果)。李>

【讨论】:

  • 我不能以某种方式将 os.listdir 用于特定目录?并且这样定义我自己的 FileSystemLocal 子类
  • 我在上面的答案中添加了一个示例。
  • 非常感谢!现在将实施!与此以及我正在构建的项目相关的另一个问题,知道如何通过套接字发送 FileSystemLocal 对象吗?
  • 不确定如何通过套接字发送它。但是要记住FileSystemLocal是基于python的os库,所以无论FileSystemLocal是在哪里创建的,它只会提供它所在的文件系统的信息。如果您在计算机A 上创建它并在计算机A 上使用它,它会告诉您计算机A 上的文件。如果您能够通过套接字将FileSystemLocal 发送到计算机B 并在计算机B 上运行它,它只会告诉您计算机B 上的文件。
  • 我的意思是创建这个对象: ObjectProperty(FileSystemLocal(), baseclass=FileSystemAbstract) 然后发送它,这样它将在计算机 B 上显示计算机 A。它会这样工作吗?如果是的话,知道如何发送吗?
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 2015-01-31
  • 2021-01-13
  • 2012-08-22
  • 2021-04-20
  • 2011-05-16
相关资源
最近更新 更多