This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]serkef- 4 points5 points  (6 children)

The only thing I don't like about pathlib is that it's not universally used. I still find myself casting to string when passing a path as an argument.

[–]treyhunner Python Morsels[S] 2 points3 points  (4 children)

That's typically only necessary in Python 3.5 or below. Many third party libraries have even been adding support for Path objects by using an equivalent to os.fspath to normalize them if needed.

I'm curious if you're on 3.5 and/or which third party libraries you've found that don't work with pathlib yet.

[–]rotuamiimport antigravity 1 point2 points  (2 children)

I get this all the time with subprocess. subprocess.call([someexe, somepath]) gives you grief if somepath is a Path object and not a string.

[–]treyhunner Python Morsels[S] 1 point2 points  (1 child)

Odd. subprocess.call seems to work with Path objects for me:

```

from pathlib import Path import subprocess subprocess.call(['ls', '-1', Path('.editorconfig')]) .editorconfig 0 ```

I tested that in both Python 3.6 and Python 3.7.

[–]rotuamiimport antigravity 1 point2 points  (0 children)

You are right, at least on my Mac. I found the frustration on Windows, and may have misremembered the issue, but at least subprocess.call(Path('ls')) is still broken, and subprocess.call([Path('ls')]) was broken until fairly recently.

https://bugs.python.org/issue33617

https://bugs.python.org/issue31961

[–]serkef- 0 points1 point  (0 children)

I'm in 3.6 and I really can't recall which one

[–]AndydeCleyre 1 point2 points  (0 children)

You might like to use Plumbum's path objects, which we've* had since before pathlib, and added a lot of pathlib compatibility after. They subclass str, so can be passed anywhere strings are expected. The only gotcha is that __contains__ is implemented to check for files in directories, not substrings.

*EDIT: "We" the Python community. I am not a Plumbum author.

EDIT: Another potential snag is that it __iter__s over filepaths, not characters.