无法使用pysftp get下载文件

时间:2018-03-13 06:43:19

标签: python pysftp

我能够成功连接sftp和pysftp,但是在将文件下载为FileNotFoundError: [Errno 2] No such file.时收到错误我还发现该文件只是在本地路径上创建添加'?'随着扩展。以下是更多细节。

ftp文件为Test_03132018080105.csv。 文件在本地路径创建为Test_03132018080105.csv?零字节

代码:

def get_move_on_ftp(ftpsource,localsource):
    if (os.stat(envvar.validfiles).st_size == 0) and (os.stat(envvar.invalidfiles).st_size == 0):
        print("There are no Source files on FTP.")
    else:
        srcinfo={'host':envvar.src_ftphost,'port':envvar.src_ftpport,'username':envvar.src_uname,'password':envvar.src_passwd}
        sftp = pysftp.Connection(**srcinfo)
        sftp.cwd(ftpsource)
        '''  Downloading Files   '''
        avail_files=open(envvar.validfiles,'r')
        for filename in avail_files:
            print(sftp.getcwd())
            #sftp.get(filename, preserve_mtime=True)
            print(filename) # for debug
            sftp.get(filename)
        sftp.close()

错误:

Traceback (most recent call last):
  File "my.py", line 96, in <module>
    main()
  File "my.py", line 92, in main
    config_file_read(config_file)
  File "my.py", line 85, in config_file_read
    get_move_on_ftp(ftpsource,localsource)
  File "my.py", line 61, in get_move_on_ftp
    sftp.get(filename)
  File "/home/username/miniconda3/lib/python3.6/site-packages/pysftp/__init__.py", line 249, in get
    self._sftp.get(remotepath, localpath, callback=callback)
  File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 770, in get
    size = self.getfo(remotepath, fl, callback)
  File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 746, in getfo
    file_size = self.stat(remotepath).st_size
  File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 460, in stat
    t, msg = self._request(CMD_STAT, path)
  File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 780, in _request
    return self._read_response(num)
  File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 832, in _read_response
    self._convert_status(msg)
  File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 861, in _convert_status
    raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] No such file

1 个答案:

答案 0 :(得分:0)

只需在ftpsource前面添加“ /”。即“ / 2020 / Jan / 10 /”。它应该可以工作