Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 480
xavier Avatar asked Nov 16 '25 02:11

xavier


1 Answers

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

str(image_path)
like image 165
glibdud Avatar answered Nov 18 '25 17:11

glibdud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!