【问题标题】:Python FTP from UNIX to Windows从 UNIX 到 Windows 的 Python FTP
【发布时间】:2013-07-02 05:31:49
【问题描述】:

我在 UNIX 服务器上运行 python 脚本。我的目标是从 UNIX 服务器获取文件并将其放入 Windows 机器。

#!/usr/bin/python
import ftplib
filename = "filename"
ftp = ftplib.FTP("xx.xxx.xxx.xxx")
ftp.login("uid", "psw")
ftp.cwd("/my/location")
print filename
ftp.retrbinary('RETR %s' % filename, open(filename, 'w').write)

我的代码目前只是从 UNIX 中的一个文件夹 ("/my/location") 中获取文件,并将其放在我运行代码的文件夹中。如何将文件放在 Windows 桌面上?

【问题讨论】:

    标签: python windows unix ftp


    【解决方案1】:

    你应该先import os然后使用os.chdir(r"\where\the\file\should\go")

    #!/usr/bin/python
    import ftplib
    import os
    filename = "filename"
    ftp = ftplib.FTP("xx.xxx.xxx.xxx")
    ftp.login("uid", "psw")
    ftp.cwd("/my/location")
    os.chdir(r"c:\somewhere")
    print filename
    ftp.retrbinary('RETR %s' % filename, open(filename, 'w').write)
    

    Windows 将桌面文件保存在何处取决于您运行的 Windows 版本,而您尚未告诉我们 - 所以我将只给您一般说明。我相信您知道如何在您的 Windows 版本中为 Windows 桌面找到正确的文件夹。

    【讨论】:

    • 伙伴 我正在文件所在的 Unix 机器上执行 python 脚本。我需要将这些文件发送到 Windows(名称:MyABC)框。当我运行代码时,我收到以下错误OSError: [Errno 2] No such file or directory: '\\\\MyComp\\c$\\my\\LOcation',因为该位置在 Windows 上,并且在这种情况下,操作系统命令会检查 Unix
    • 不是我想要的。但答案很有帮助,因为它帮助我了解更多。因此接受为正确答案。
    • 对不起,我误读了这个问题,并认为您在 Windows 端运行脚本,否则ftp.cwd("/my/location") 是完全错误的——正如您现在发现的那样。基本上,使用ftp.cwd()更改远程端的工作目录,使用os.chdir()更改本地端的工作目录。因此,您可以使用ftp.cwd(r"C:\Windows\Desktop")(或者您的 Windows 桌面实际位于的任何位置 - 将适当的路径替换为您的 specific 版本。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多