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

12,137

Solution 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

Solution 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.

Share:
12,137
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin almost 2 years

    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

  • ezdazuzena
    ezdazuzena over 9 years
    For the sake of completeness: to switch back to ASCII use TYPE A
  • rayzinnz
    rayzinnz over 2 years
    works with pyftpdlib server too