IOError: [Errno 13] Permission denied: or IOError: [Errno 2] when using pysftp module or paramiko module

Recently, I have to use python to write a program to upload files from the local to the FTP server , and download files from the server to the local process. First of all, I checked the python modules that need to be used on the Internet,

See that it can be achieved using the put and get methods in the pysftp and paramiko modules. So, I tried it, but inexplicably there were many problems, some of which were IOError: [Errno 13] Permission denied:,
some were IOError: [Errno 2] No such file, put() sftp_client, and some had IOError : [Errno 22] invalid mode ('rb') or filename is abnormal, and there are other types of errors. In short, after
struggling for about three days, these problems were finally solved, and the way of solving them may be unbearable. The following are the problems encountered in this process, and the solutions are summarized as follows:

1. IOError: [Errno 2] No such file, put() sftp_client problem and solution

srv = pysftp.Connection(~)
srv.put(localPath,remotePath)

The above method did not upload successfully, but the change to the following method turned out to be successful.

srv = pysftp.Connection(~)
srv.chdir(remotePath)
srv.put(localPath)

The specific code is as follows:

The original code where the exception occurred:

sftp.put('C:/Users/Administrator/Desktop/test.doc','/root/myTest/testdata/')

After modification, the test.doc file under the local path ' C:/Users/Administrator/Desktop/test.doc ' can be successfully uploaded to the server '/root/myTest/testdata/' path:

  1. with pysftp.Connection('10.2.19.12', username='root', password='yunjikeji', cnopts=cnopts) as sftp:
  2. sftp.chdir('/root/myTest/data1')
  3. sftp.put('C:/Users/Administrator/Desktop/test.doc')

2. IOError: [Errno 22] invalid mode ('wb') or filename or IOError: [Errno 13] Permission denied: problem

When this kind of problem occurs, some of the reference materials say that the format of the path is a problem. You need to add r before the path or replace the '\' in the local path with '/', because POSIX-like file system use '/'.

sftp.get('/root/myTest/data1/', 'C:/Users/Administrator/Desktop/python.pdf', callback=None, preserve_mtime=False)#Here replace the '\' in the local file path with '/'The problem is solved, but a new problem occurs "IOError: Failure"

Finally, after a few days of tossing and re-reading the API documentation, I found that the upload and download methods are as follows:

get(remotepath, localpath=None, callback=None, preserve_mtime=False)

Note: The remotepath and localpath here are: the complete file path in the form of "the directory where the file is located + '/' + file name", not the directory with only the file, otherwise various errors of the types listed above will occur.

put(localpath, remotepath=None, callback=None, confirm=True, preserve_mtime=False)

Note: The remotepath and localpath here are: "the directory where the file is located + '/' + file name" The complete file path is not a directory with only files, otherwise various errors will occur.

Change the above directory to the full path of the file to be uploaded and downloaded, whether using the put or get method in the pysftp or paramiko module (note: the path here includes the file name, otherwise the program does not know where to store the file) , so there will be various errors), the various problems that appear above will disappear.

After struggling for so long, it was because the file path was written as a directory, so that various exceptions occurred and it took so long, which was unbearable. It may be that when writing hdfs file upload and download a few days ago, the path of the directory in the upload and download methods inside is confused with the full path of the file in the get and put methods here. To sum up, if you encounter problems in the future, if you still can't solve it after looking up various methods for more than a few hours, you should just look at the official API documentation directly and practically. Bar.




Related: IOError: [Errno 13] Permission denied: or IOError: [Errno 2] when using pysftp module or paramiko module