简体   繁体   中英

Unable to download file with pysftp get

I am able to connect the sftp with pysftp successfully, but getting error while downloading file as FileNotFoundError: [Errno 2] No such file. I also observed that the file is just creating at local path adding '?' along with extension. Below are more details.

File at ftp as Test_03132018080105.csv. File creating at Local path as Test_03132018080105.csv? with zero bytes

Code:

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()

Error:

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

Just add "/" in front of ftpsource. ie "/2020/Jan/10/". It should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM