17

This error appeared when trying to use the 'tmpdir' in a pytest test.

TypeError: object of type 'LocalPath' has no len()

2 Answers 2

23

'tmpdir' is of type <class 'py._path.local.LocalPath'>, just wrap 'tmpdir' in a string when passing to os.path.join

example:

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

1
  • 5
    Or even: str(tmpdir / 'my_test_file.txt') Commented Nov 20, 2014 at 21:09
4

Alternatively you can directly access the string form of LocalPath as an attribute.

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

I used to think that using the attribute access meant that you were not casting the object to a string thus being more efficient but I think I am wrong in that assumption however, I like this style a bit better it is easier to write IMHO

Not the answer you're looking for? Browse other questions tagged or ask your own question.