Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.join fails with "TypeError: object of type 'LocalPath' has no len()"

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

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

like image 687
Efren Avatar asked Nov 20 '14 07:11

Efren


2 Answers

'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')

like image 128
Efren Avatar answered Oct 07 '22 00:10

Efren


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

like image 31
Jon Sonesen Avatar answered Oct 06 '22 22:10

Jon Sonesen