Reputation: 787
Hi I am trying to copy a remote file in a server to a local location using Paramiko's SFTP client. Here below is the code.
try:
self.SFTP.get(remotepath, localpath, callback=None)
except IOError as e:
print "File Not Found "+self.location
Remote location doesn't always contains the file requested so I want to print the Error message and end the process.
Unfortunately it prints the message(IOError message) but it also creates the local file with zero size.
Is this a bug or is there any other way to avoid this?
Upvotes: 1
Views: 2634
Reputation: 863
I would use:
sftp.stat(remotepath)
So, in your sample code:
try:
if self.SFTP.stat(remotepath):
self.SFTP.get(remotepath, localpath, callback=None)
except IOError as e:
print "File Not Found "+self.location
Upvotes: 2
Reputation: 156
It's expected.
Instead trying to get a file that you don't know if it exist, I suggest you either:
listdir
command, or file
command.
close
the SFTPFile object, and download the file with the get
command.Upvotes: 1