I try to transfer a file between two servers. I used Paramiko. There is a directry called "data" but it says:
IsADirectoryError: [Errno 21] Is a directory: '/home/mmoradi2/data/'
I have no idea what is the problem. This is my code:
import paramiko
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname='****8', username='****',password='****',port=22)
sftp = s.open_sftp()
list_files_share_drive = sftp.listdir('./')
# Looping through the files and transferring them to the data folder and importing them
the_file = list_files_share_drive[0]
sftp.get( '/home/sftpwis/'+ the_file , '/home/mmoradi2/data/')
I changed the target ('/home/mmoradi2/data/') to: '/home/mmoradi2/data' and '/home/mmoradi2'
It did not work and I had the same error. I changed it to '/home/mmoradi2/Data/'. then I had no error but there is a new directry that I cannot cd to it!!! (cannot open it)
How can I fix this problem?
The second argument to the SFTPClient.get()
method is the local path where the file should be put. This is not the directory that it should be put into, but the actual file name that it should be copied to. The fix is simply to change your line to
sftp.get( '/home/sftpwis/'+ the_file , '/home/mmoradi2/data/' + the_file)
The error message is fairly reasonable - it says that the path you have given is a directory; that implies that it is expecting a path that is not a directory, i.e., it is a regular file name.
The Data
entry you have is not a directory (see how it is a different colour to the other directories?), it is the file that you have retrieved from the remote server.