AttributeError: 'PosixPath' object has no attribute 'endswith'
时间: 2023-09-11 22:06:22 浏览: 302
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
5星 · 资源好评率100%
This error occurs when you try to use the method "endswith()" on a PosixPath object, which is not supported.
To fix this error, you can convert the PosixPath object to a string using the "str()" method, and then use the "endswith()" method on the resulting string.
For example:
```
import pathlib
path = pathlib.Path('/path/to/file.txt')
# Convert the PosixPath object to a string
path_str = str(path)
# Use the "endswith()" method on the string
if path_str.endswith('.txt'):
print('File is a text file.')
else:
print('File is not a text file.')
```
Output:
```
File is a text file.
```
阅读全文