代码之家  ›  专栏  ›  技术社区  ›  Ratha

python ioerror:[errno 21]是一个目录

  •  0
  • Ratha  · 技术社区  · 5 年前

    这是我的示例python脚本,我想在其中从sftp服务器下载一个文件到本地。

    srv = pysftp.Connection(host=host, username=username, password=password, port=port, cnopts=connOption)
        with srv.cd(sftppath):
            data = srv.listdir()
            try:
                for infile in data:
                    print infile
                    srv.get(infile, destination, preserve_mtime=True)
    

    我可以成功连接,它列出文件夹中的所有文件。但当我使用srv.get()将AOD下载到桌面时,会出现以下错误:

    IOError: [Errno 21] Is a directory: '/Users/ratha/Desktop'
    

    误差叠加;

    Traceback (most recent call last):
      File "/Users/ratha/PycharmProjects/SFTPDownloader/handler.py", line 9, in <module>
        main()
      File "/Users/ratha/PycharmProjects/SFTPDownloader/handler.py", line 5, in main
        downloadSFTPFiles()
      File "/Users/ratha/PycharmProjects/SFTPDownloader/Utilities/SFTPConnector.py", line 49, in downloadSFTPFiles
        srv.get(infile, destination, preserve_mtime=True)
      File "/Users/ratha/PycharmProjects/SFTPDownloader/venv/lib/python2.7/site-packages/pysftp/__init__.py", line 249, in get
        self._sftp.get(remotepath, localpath, callback=callback)
      File "/Users/ratha/PycharmProjects/SFTPDownloader/venv/lib/python2.7/site-packages/paramiko/sftp_client.py", line 801, in get
        with open(localpath, "wb") as fl:
    IOError: [Errno 21] Is a directory: '/Users/ratha/Desktop'
    

    我在这里做错什么了?

    2 回复  |  直到 5 年前
        1
  •  1
  •   Imperishable Night    5 年前

        with open(localpath, "wb") as fl:
    IOError: [Errno 21] Is a directory: '/Users/ratha/Desktop'
    

    pysftp /Users/ratha/Desktop documentation

    os.path.join('/Users/ratha/Desktop', filename)

        2
  •  1
  •   blhsing    5 年前

    destination

    import os
    
    ...
    
    srv.get(infile, os.path.join(destination, os.path.basename(infile)), preserve_mtime=True)