【问题标题】:Downloading file with a space in filename using ftplib使用ftplib下载文件名中带有空格的文件
【发布时间】:2020-12-20 15:57:02
【问题描述】:

我正在从 C-More 工业 HMI FTP 服务器下载两个文件。我不知道 HMI 运行的是什么操作系统,但我怀疑它的 FTP 服务器有一些怪癖。使用 Jython 2.7,可以毫无困难地读取一个文件,但另一个文件的文件名中有一个空格,并且正常的 wrap-in-quotes 解决方案还不起作用。

以下内容适用于 Windows 10 FTP 客户端。

ftp> get NO_SPACES.csv
200 PORT command successful.
150 Opening ASCII mode data connection for NO_SPACES.csv.
226 Transfer complete.
ftp: 12774 bytes received in 0.27Seconds 47.66Kbytes/sec.

ftp> get "WITH SPACE.csv"
200 PORT command successful.
150 Opening ASCII mode data connection for WITH SPACE.csv.
226 Transfer complete.
ftp: 6328 bytes received in 0.02Seconds 316.40Kbytes/sec.

到目前为止,一切都很好。现在在 Python 中尝试一下:

ftp = FTP(myIP)                         # Connect.
ftp.login(userName, password)           # Login.
ftp.set_pasv(False)                     # Required by the C-More panel for some reason.
with io.BytesIO() as binary_buffer:
    # read all of products into a binary buffer
#   ftp.retrbinary("RETR NO_SPACES.csv", binary_buffer.write)       # This line works.
    ftp.retrbinary('RETR "WITH SPACE.csv"', binary_buffer.write)    # This one doesn't.

我的开发系统中的脚本控制台报告:

ftplib.error_perm: 550 "WITH SPACE.csv": 未采取请求的操作。

  1. 已更改文件名以保护无辜者。
  2. Windows FTP 喜欢get 命令。 Python 似乎偏爱RETR。
  3. 我试过'RETR "WITH SPACE.csv"' 和"RETR 'WITH SPACE.csv'"。结果相同。
  4. 如果必须,我可以重命名 HMI 中的文件,但这需要一些验证和文书工作,这并不好玩。
  5. 我正在使用 Jython 2.7 的 Inductive Automation 的 Ignition! SCADA 系统的最新版本开发此功能。

有人有什么想法让我试试吗?

【问题讨论】:

    标签: python ftp jython ftplib


    【解决方案1】:

    ftplib 没有空格问题。问题是您添加到 RETR 命令的引号。不应该有引号:

    ftp.retrbinary('RETR WITH SPACE.csv', binary_buffer.write)
    

    如果您使用-d 开关在ftp 中启用调试模式,您将看到它也不会在RETR 命令中向FTP 服务器发送引号:

    ftp> get "WITH SPACE.csv"
    ---> PORT 127,0,0,1,15,145
    200 Port command successful
    ---> RETR WITH SPACE.csv
    150 Opening data channel for file download from server of "/WITH SPACE.csv"
    226 Successfully transferred "/WITH SPACE.csv"
    ftp: 12 bytes received in 0.00Seconds 12000.00Kbytes/sec.
    

    请注意,get 是命令行 ftp 客户端 user 命令,可转换为 FTP protocol RETR 命令。

    【讨论】:

    • 呃! Windows 10 命令行 FTP 客户端确实需要它们。我认为没有它们尝试 ftplib 没有任何意义。谢谢!
    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多