像对象一样访问目录
pyerarch的Python项目详细描述
pyerarchy是一个python包,用于简单的文件系统浏览和操作。它允许与文件系统交互 直接作为python对象,而不隐式调用ospackage。最后,它使代码更具可读性 很好。
基本用法
阴间的中心点是节点。基本用法从初始化入口点节点和操作 更进一步:
node = Node('entry/point/path') # Initialize a new node children = node.ls() # List all child entities of the node node.mkdir('newdir').mkdir('anotherdir') # Create a new directory under node, and another one under the new one :) # Now access the newly created directories anotherdir = node/'newdir'/'anotherdir' # This is the most common way to access nodes children = node.newdir.anotherdir.ls() # This method can be used to invoke an operation on the result node # What about files? myfile = node.myfile # And even... myfile = node.myfile.open('w') # Or... contents = node.myfile.read() # ...which handles everything # What if the filename contains a dot, dash, etc? # Well... myfile_node = node/'filename.with.dots-and-dashes' # And then do your stuff on myfile_node with myfile_node.open('r') as f: ... # Or again... contents = myfile_node.read()
pyerarchy的另一个有用功能是在python模块中非常方便地与模块中包含的静态文件进行交互:
static_data_node = ThisModuleNode()/'path/to/the/static/data/relative/to/module' some_file_node = static_data_node/'some/static/text/file' contents = some_file_node.read()