Python代替os.path使用哪个路径模块或类? [英] Which path module or class do Python folks use instead of os.path?

查看:129
本文介绍了Python代替os.path使用哪个路径模块或类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道有多少人在Python中使用诸如Jason Orendorff的模块之类的路径模块,而不是使用os.path来联接和分割路径?您是否使用过:

Just wondering how many people use a path module in Python such as Jason Orendorff's one, instead of using os.path for joining and splitting paths? Have you used:

  • Jason's path module (updated for PEP 355)
  • Mike Orr's Unipath, basically a more modern version of the above
  • Noam Raphael's alternative path module that subclasses tuple instead of str

我知道Jason的路径模块被制成 PEP 355 并被拒绝BDFL.似乎主要是因为它试图在一堂课中做所有事情.

I know Jason's path module was made into PEP 355 and rejected by the BDFL. This seems like it was mainly because it tried to do everything in one class.

我们的用例主要是为了简化路径的连接和拆分,因此,如果这样的路径类仅实现操作的拆分/连接类型,我们将非常高兴.谁不想这样做:

Our use case is mainly to simplify joining and splitting components of paths, so we'd be quite happy if such a path class only implemented the split/join type of operations. Who wouldn't want to do this:

path(build_dir, path(source_file).name)

或者这个:

build_dir / path(source_file).name

代替此:

os.path.join(build_dir, os.path.basename(source_file))

推荐答案

我可以毫不犹豫地选择一个Python程序并解释当前的标准方法-它是明确的并且没有歧义:

I can pick up a Python program and interpret the current standard method without hesitation - it's explicit and there's no ambiguity:

os.path.join(build_dir, os.path.basename(source_file))

Python的动态类型使得在阅读时很难理解第一种方法:

Python's dynamic typing makes the first method rather difficult to comprehend when reading:

build_dir / path(source_file).name

此外,分割字符串并不常见,这会引起更多的混乱.我怎么知道那两个不是整数?还是漂浮?如果两种类型都以非字符串类型结尾,则在运行时不会出现TypeError.

Plus it's not common to divide strings, which brings up more confusion. How do I know that those two aren't integers? Or floats? You won't get a TypeError at runtime if both end up as non-string types.

最后,

path(build_dir, path(source_file).name)

有什么比os.path方法更好的方法吗?

How is that any better than the os.path method?

尽管它们可以简化"编码(即,使编写变得更容易),但是如果其他不熟悉替代模块的人需要维护代码,您将陷入困境.

While they may "simplify" coding (ie, make it easier to write), you're going to run into strife if someone else who is unfamiliar with the alternative modules needs to maintain the code.

所以我想我的答案是:我不使用替代路径模块. os.path已具备我所需要的一切,并且界面还不错.

So I guess my answer is: I don't use an alternative path module. os.path has everything I need already, and it's interface isn't half bad.

这篇关于Python代替os.path使用哪个路径模块或类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆