Python os.path.join失败,返回“0”;TypeError:类型为'的对象;本地路径';没有len();

Python os.path.join失败,返回“0”;TypeError:类型为'的对象;本地路径';没有len();,python,pytest,os.path,Python,Pytest,Os.path,在pytest测试中尝试使用“tmpdir”时出现此错误 TypeError:类型为“LocalPath”的对象没有len() “tmpdir”的类型为,在传递到os.path.join时,只需将“tmpdir”包装成字符串即可 例如: os.path.join(str(tmpdir),'my_test_file.txt')或者,您可以直接访问LocalPath的字符串形式作为属性 os.path.join(tmpdir.strpath,“my\u test\u file.txt”) 我曾经认

在pytest测试中尝试使用“tmpdir”时出现此错误

TypeError:类型为“LocalPath”的对象没有len()


“tmpdir”的类型为
,在传递到
os.path.join时,只需将“tmpdir”包装成字符串即可

例如:


os.path.join(str(tmpdir),'my_test_file.txt')
或者,您可以直接访问LocalPath的字符串形式作为属性

os.path.join(tmpdir.strpath,“my\u test\u file.txt”)
我曾经认为,使用属性访问意味着您没有将对象强制转换为字符串,因此效率更高,但我认为我的假设是错误的。然而,我更喜欢这种样式,它更容易编写IMHO

甚至:
str(tmpdir/'my_test_file.txt')