os.path.join失败,出现"TypeError:类型为'LocalPath'的对象没有len()"

Efr*_*ren 11 python pytest os.path

尝试在pytest测试中使用'tmpdir'时出现此错误.

TypeError:"LocalPath"类型的对象没有len()

Efr*_*ren 16

'tmpdir'是类型<class 'py._path.local.LocalPath'>,只是在传递给字符串时将'tmpdir'包装os.path.join

例:

os.path.join(str(tmpdir), 'my_test_file.txt')

  • 甚至:`str(tmpdir /'my_test_file.txt')` (4认同)