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