how can I monkey-patch pathlib on python 3.5, 3.6, 3.7 to get same functionality like in python 3.8?

Viewed 302

I use pathlib in my software for library development - but unfortunately my travis matrix brakes, because on some older python versions (3.5 - 3.7) the pathlib.Path is not compatible with the newest 3.8 Version.

for instance, following will work on python 3.8, but not on 3.6, 3.7 etc. :

>>> path_test_file = pathlib.Path('./tests/mytest.txt')
>>> path_test_file.unlink(missing_ok=True)

Is there a convenient way to upgrade the pathlib library via requirements.txt, pip, etc ?

I tried also to monkey-patch the pathlib of older versions, but failed miserably.

As @jakub points out, of course there are programmatic solutions for that, like :

if path_test_file.exits(): 
    path_test_file.unlink()

# or:
try:
    path_test_file.unlink()
except Exception:
    pass

that is really all clear and does not need an answer - but I don't want to clutter my code like that. I would rather like to include some module and monkey-patch pathlib.Path if needed, and leave all other code as it is - and still be compatible with older python versions.

Maybe I should bump up https://github.com/KenKundert/extended_pathlib/blob/master/extended_pathlib.py

1 Answers

I just copied the python3.8 pathlib file to a new package, and tweaked a few lines, so it can behave like pathlib.

Now You can use all the python3.8 pathlib goodies also in 3.6, 3.7

You might find it on github or pypi


Disclaimer: I'm the author of the pathlib3x library.

Related