python - 如何将文件结构表示为python对象
问题描述
我正在尝试找到一种将文件结构表示为 python 对象的方法,这样我就可以轻松获得特定路径,而无需输入所有字符串。这适用于我的情况,因为我有一个静态文件结构(不改变)。
我想我可以将目录表示为类,将目录中的文件表示为类/静态变量。
我希望能够浏览 python 对象,以便它返回我想要的路径,即:
print(FileStructure.details.file1) # root\details\file1.txt
print(FileStructure.details) # root\details
我从下面的代码中得到的是:
print("{0}".format(FileStructure())) # root
print("{0}".format(FileStructure)) # <class '__main__.FileStructure'>
print("{0}".format(FileStructure.details)) # <class '__main__.FileStructure.details'>
print("{0}".format(FileStructure.details.file1)) # details\file1.txt
我到目前为止的代码是......
import os
class FileStructure(object): # Root directory
root = "root"
class details(object): # details directory
root = "details"
file1 = os.path.join(root, "file1.txt") # File in details directory
file2 = os.path.join(root, "file2.txt") # File in details directory
def __str__(self):
return f"{self.root}"
def __str__(self):
return f"{self.root}"
我不想实例化这个类来完成这项工作。我的问题是:
- 如何调用类对象并让它返回一个字符串而不是 <class ....> 文本
- 如何让嵌套类使用它们的父类?
解决方案
让我们开始吧:你可能实际上并不想要这个。Python3 的pathlib
API 似乎比这更好,并且已经得到广泛支持。
root = pathlib.Path('root')
file1 = root / 'details' / 'file1' # a Path object at that address
if file1.is_file():
file1.unlink()
else:
try:
file1.rmdir()
except OSError as e:
# directory isn't empty
但是如果你因为某种原因死心塌地,你需要重写__getattr__
以创建一个新FileStructure
对象并跟踪父母和孩子。
class FileStructure(object):
def __init__(self, name, parent):
self.__name = name
self.__children = []
self.__parent = parent
@property
def parent(self):
return self.__parent
@property
def children(self):
return self.__children
@property
def name(self):
return self.__name
def __getattr__(self, attr):
# retrieve the existing child if it exists
fs = next((fs for fs in self.__children if fs.name == attr), None)
if fs is not None:
return fs
# otherwise create a new one, append it to children, and return it.
new_name = attr
new_parent = self
fs = self.__class__(new_name, new_parent)
self.__children.append(fs)
return fs
然后将其用于:
root = FileStructure("root", None)
file1 = root.details.file1
您可以添加一个__str__
和__repr__
来帮助您的陈述。你甚至可以包含一个path
属性
# inside FileStructure
@property
def path(self):
names = [self.name]
cur = self
while cur.parent is not None:
cur = cur.parent
names.append(cur.name)
return '/' + '/'.join(names[::-1])
def __str__(self):
return self.path
推荐阅读
- sql - 添加 order by 子句时 SQL Server 查询卡住
- bash - 从 Atom 运行 bash 脚本时,stdout 和 stderr 乱序
- azure - 使用 VScode Azure Cognitive Services 扩展一次在索引中插入多个文档。(Azure 搜索服务)
- r - 在 Rshiny 中使用 Rcolorpicker 来获取不断变化的调色板
- amazon-cloudformation - 不允许使用无关密钥 [HealthCheck]
- html - Django - CSS 没有样式化 HTML 页面
- powerbi - 上个月同期
- python - 如何计算每个霍夫曼码的长度?
- node.js - 角度错误:Node Sass 版本 6.0.0 与 ^4.0.0 不兼容。角
- bash - 为什么上面的输出是 2 而不是 6 而另一个是 3?