我正在尝试找到一种将文件结构表示为 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
关于python - 如何将文件结构表示为 python 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54227232/