Python ftplib can't get size of file before download?

江枫思渺然 提交于 2019-12-19 05:44:11

问题


I'm using ftplib to transfer files. Everything is working great. Now I'm trying to get the size of the target file before downloading.

  1. First, I tried just getting size with ftp.size(filename). Server complained that I can't do that in ascii mode.

  2. Then I tried setting binary mode using ftp.sendcmd("binary") and ftp.sendcmd("bin"). In both cases the server complained "500 binary Not understood"

Can ftplib get size of a file before downloading in this instance? I don't control the FTP server and can't change how it's behaving.

Thanks


回答1:


Very late reply, but here's the correct answer. This works with ProFTPD.

ftp.sendcmd("TYPE i")    # Switch to Binary mode
ftp.size("/some/file")   # Get size of file



回答2:


Ftplib can get the size of a file before downloading. As the documentation says:

FTP.size(filename) Request the size of the file named filename on the server. On success, the size of the file is returned as an integer, otherwise None is returned. Note that the SIZE command is not standardized, but is upported by many common server implementations

Apparently your server doesn't support this feature.




回答3:


"Server complained that I can't do that in ascii mode." -- Try showing the exact code that you used AND the exact text of the server response. Use copy/paste, don't type from memory.

Do you have access to a command-line FTP client? If not, get one. Use it to experiment with what the server can do. A client command like REMOTEHELP is your friend. Example:

ftp> remotehelp size
214 Syntax: SIZE <sp> pathname

This indicates that the server to which I was connected will support a SIZE command.



来源:https://stackoverflow.com/questions/3231910/python-ftplib-cant-get-size-of-file-before-download

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!