【问题标题】:uploading .txt to FTP server adds new lines将 .txt 上传到 FTP 服务器会添加新行
【发布时间】:2020-05-03 13:37:47
【问题描述】:

我正在尝试使用 Python 和 ftplib 将 .txt 文件上传到 FTP 服务器。连接作品,上传作品 - 只是有点奇怪。

我的文件如下所示:

line1
line2
line3
...

它是通过简单的写入创建的 file.write('line1\n) ...

我使用 storbinary 上传它(与 storlines 相同)

ftp = ftplib.FTP() # and connecting to the server

file = open(file_name, 'rb')
ftp.storbinary('STOR file.txt', file)

现在,当我查看服务器中的文件时(也尝试下载它然后查看),它看起来像这样:

line1

line2

line3

...

为什么会这样?它必须在上传时发生,从那时起它看起来就像它的预期那样。

【问题讨论】:

  • .xml 也是如此 - 似乎没有链接到文件类型

标签: python ftp newline ftplib


【解决方案1】:

您是在 Windows 下创建此文件并发送到 Linux FTP 服务器,对吗? Windows 以\r\n 结束行,Linux 以\n 结束行。使用storlines 而不是storbinary 应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多