Which module or class of path do Python people use instead of os.path?

Just wondering how many people use the path module in Python, like Jason Orendorff, instead of using os.path to connect and split paths? You used:

I know that Jason's path module was translated into PEP 355 and rejected by the BDFL. It seems that this was mainly because he tried to do everything in one class.

Our precedent is mainly to simplify the joining and splitting of path components, so we would be happy if such a path class implements only split / join operations. Who would not want to do this:

 path(build_dir, path(source_file).name) 

or that:

 build_dir / path(source_file).name 

instead of this:

 os.path.join(build_dir, os.path.basename(source_file)) 
+7
python path
source share
3 answers

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

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

Python's dynamic typing makes the first method pretty hard to read when reading:

 build_dir / path(source_file).name 

Plus it’s not often for line splitting, which causes more confusion. How do I know that these two are not whole? Or swims? You will not get a TypeError at runtime if both end as non-string types.

Finally,

 path(build_dir, path(source_file).name) 

How is this better than the os.path method?

While they can “simplify” the encoding (that is, make it easier to write), you will run into revelry if someone who is not familiar with alternative modules needs to support the code.

So, I think my answer is: I am not using an alternative path. os.path has everything I need and the interface is not bad.

+11
source share

A simple but useful trick:

import os

Path = os.path.join

Then instead:

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

You can do it:

Path (build_dir, Path (source_file))

+2
source share

Separating lines on the join path may seem like a "neat trick", but this is exactly what Python programmers like to avoid (and by the way, programmers in most other langauges.) The os.path module is widely used and easily understood by everyone. Doing funky with overloaded operators, on the other hand, is confusing, it impairs the readability of your code, which should be one of Python's strengths.

C ++ programmers, on the other hand, love such things. It is possible that one of the reasons for C ++ code can be so difficult to read.

-one
source share

All Articles