Unexpected TypeError when using Python 3.8's `pathlib` with `os` functions
I recently switched to I'm integrating two systems and I'm working with a `TypeError` when trying to use Python 3.8's `pathlib.Path` objects with some `os` functions. Specifically, I have a function that attempts to remove files using `os.remove()` and I'm passing a `Path` object to it. Here's the relevant code snippet: ```python import os from pathlib import Path file_to_remove = Path('example.txt') if file_to_remove.exists(): os.remove(file_to_remove) # This line raises TypeError ``` When I run this code, I get the following behavior: ``` TypeError: expected str, bytes or os.PathLike object, not PosixPath ``` I expected that `pathlib.Path` would be compatible with `os.remove()`, as both are part of the standard library. I've also tried converting the `Path` object to a string explicitly: ```python os.remove(str(file_to_remove)) # This works fine ``` However, I want to understand why the direct use of `Path` leads to a `TypeError`. Is there a best practice for mixing `pathlib` with `os` functions or should I always convert them to strings? Additionally, are there any performance implications in using one over the other in certain scenarios? Thanks in advance for your insights! For context: I'm using Python on macOS. Could someone point me to the right documentation? I've been using Python for about a year now. This is for a application running on Linux.