Super_Py_Me
Super_Py_Me

Reputation: 169

Python TypeError cannot concatenate str and 'xxxxx' objects

I am getting the following results when I try to 'get' my remote files. I can print and I know the files are there. Do I need to build the files into a list?

file_list_attr = sftp.listdir_attr()
for pfile in file_list_attr:
    if DT.datetime.fromtimestamp(pfile.st_mtime) > yesterday:
       # print pfile
       localfile = path + "\\" + pfile
       sftp.get(pfile,localfile) 



Traceback (most recent call last):
  File "C:\Documents and Settings\tyoffe\Desktop\FTP", line 41, in <module>
localfile = path + "\\" + pfile
TypeError: cannot concatenate 'str' and 'SFTPAttributes' objects

Upvotes: 1

Views: 5014

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121744

You need to use the .filename property instead, see the SFTPAttributes documentation:

file_list_attr = sftp.listdir_attr()
for pfile in file_list_attr:
    if DT.datetime.fromtimestamp(pfile.st_mtime) > yesterday:
       # print pfile
       localfile = path + "\\" + pfile.filename
       sftp.get(pfile,localfile) 

Upvotes: 1

Mike Vella
Mike Vella

Reputation: 10575

The key is in the last line:

TypeError: cannot concatenate 'str' and 'SFTPAttributes' objects

The error is telling you that the SFTPAttributes object is not a string, and that a string can only be concatenated with a string. I'm not sure how to typecast SFTPAttributes to a string, things you can try are:

str(pfile)

or something like pfile.tostring()

What works best will usually depend on the details of the object itself.

Upvotes: 0

Related Questions