I’m trying to write a small Python script that will get query results from a database, write them to a file, and then sftp the file to a different server. The pieces work just fine but I’m getting a weird error when trying to sftp the file immediately after it’s written.
The error I’m getting is
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 558, in put
file_size = os.stat(localpath).st_size
TypeError: coercing to Unicode: need string or buffer, file found
The offending line of code is just
sftp.put(outputfile, sftpoutputfile)
I tried using a copy of the output file instead of the one that’s being written in the script and that worked exactly as it’s supposed to. I’m calling file.close() after the file is written (and before setting up the sftp) so it seems like the file should be, well, closed and usable after that. Can someone tell me what I’m doing wrong? I can post more of the code if that would be helpful. Thank you very much.
The error message is telling you that it (in this case, os.stat) wants a stringlike object, and you’re giving it the file instead.
Looking at the source of sftp_client.py in my copy of paramiko, we see
so I’m pretty sure that it wants the filename, not the file itself.