Os.path.join does not work with "TypeError: object of type" LocalPath "does not have len ()"

This error occurred while trying to use tmpdir in the pytest test.

TypeError: an object of type 'LocalPath' does not have len ()

+6
source share
2 answers

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

Example:

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

+12
source

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

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

I used to think that using attribute access means that you did not add the object to the string, thereby being more efficient, but I think I am mistaken in this assumption, but I like this style a little better, it's easier to write IMHO

+2
source

Source: https://habr.com/ru/post/978443/


All Articles