user8953650
user8953650

Reputation: 1020

Python Posix path cannot concatenate str

Using this piece of code, suggest me :

files = [item for item in Path('my_directory').iterdir() 
         if item.is_file()
         and item.name.startswith('str')]

I cannot use the element of list to concatenate the element ... this is the error :

   self.bins.append(np.genfromtxt('vorcospdf/56.72/'+j ,usecols=(0,), skip_header=0)) 
TypeError: can only concatenate str (not "PosixPath") to str

Upvotes: 1

Views: 5001

Answers (1)

Barmar
Barmar

Reputation: 782488

files is a list of PosixPath objects, not strings. Use str() to convert them to strings.

files = [str(item) for item in Path('my_directory').iterdir() 
         if item.is_file()
         and item.name.startswith('str')]

Upvotes: 2

Related Questions