像对象一样访问目录

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()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java是否存在这样一种场景:Java7的Hashmap实现优于Java8的实现   java如何打印这些完整的数组?   java BuildException:未能执行aapt   java如何使用类。未知类型的集合返回的cast()   java准备语句返回错误的值   webview 安卓中的java显示身份验证对话框   java如何重命名列名JPA Hibernate   java查询从特定用户和特定时间段的Oracle表(通过比较原始表和备份表)中选择修改的行   java Youtube视频仅在三星S6设备上加载时自动暂停   未调用java RecyclerView getItemViewType   使用JSch setCommand执行带有源选项的java Shell ping命令时失败   java Hibernate:无法删除|删除分离的实例   Java Spring@MappedSuperclass字段作为子类中的@Id字段   java Android:确定单击了哪个按钮,因为该按钮未在xml中定义   如何计算java阵列内存使用率   使用Java查找按字母顺序排列的第一个字符串   javascript注销功能刷新页面,但页面仍已登录   当接口作为参数提供时,java依赖项注入不起作用   java中带原语的字符串扭曲