尝试在 pytest 测试中使用“tmpdir”时出现此错误。
TypeError:“LocalPath”类型的对象没有 len()
'tmpdir' 是类型<class 'py._path.local.LocalPath'>
,传递给时只需将 'tmpdir' 包裹在一个字符串中os.path.join
例子:
os.path.join(str(tmpdir), 'my_test_file.txt')
或者,您可以直接访问 LocalPath 的字符串形式作为属性。
os.path.join(tmpdir.strpath, 'my_test_file.txt')
我曾经认为使用属性访问意味着您没有将对象转换为字符串,因此效率更高,但我认为我的假设是错误的,但是我更喜欢这种风格,更容易编写恕我直言