Python TypeError无法连接str和'xxxxx'对象

时间:2013-01-15 15:46:02

标签: python python-2.7

当我尝试'获取'我的远程文件时,我得到以下结果。我可以打印,我知道文件在那里。我是否需要将文件构建到列表中?

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

2 个答案:

答案 0 :(得分:1)

您需要使用.filename属性,请参阅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) 

答案 1 :(得分:0)

关键是在最后一行:

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

错误告诉您SFTPAttributes对象不是字符串,并且字符串只能与字符串连接。我不确定如何将SFTPAttributes强制转换为字符串,您可以尝试的是:

str(pfile)

或类似pfile.tostring()

最有效的方法通常取决于对象本身的细节。