'How can I convert a pathlib.Path object to a string?

I read that pathlib is the new Python way to deal with paths.

So I do:

with open(pic_name, 'wb') as image:
    image.write(download.content)
    image_path = Path(pic_name).resolve()
    return image_path

When I print image_path I get the full path to the image, but when I try to pass it to a function that uses ffmpeg to create a video file, I get:

TypeError: Can't convert 'PosixPath' object to str implicitly

I suspect this is because the object is Posix and the ffmpeg shell command expects a string.

In other cases I also got related error messages like

TypeError: 'PosixPath' object does not support indexing

or

TypeError: object of type 'PosixPath' has no len()

So how do you transform a Posix path to a string?



Solution 1:[1]

Python can't do it implicitly, but you can do it explicitly:

str(image_path)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 glibdud