Python Forum

Full Version: FTP file size
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

Do you know a working solution to compare the remote file with the local file before downloading it from the ftp server?

I tried md5 hashing on both end but it is not supported via FTP, and I os.stat() or FTP.size(f) dont return the remote file's size either.
Locally I prefer:
>>> import os
>>> s = os.path.getsize("C:/WinPEpge.sys")
>>> s
536870912


But idk about your FTP, you gave no intel on what libs and soft are you using there.
You could ie use python interpreter on remote host to calculate it via running cmd, then compare returned result to the file from your host. Also, I guess if you are using some python ftp lib, it has to be there something for checking object size in docs.
You can try to get meta data Content-Length,to get size before download.
Now can it be that you need to log in to Ftp server then can use ftplib .
Example:
import requests

url = 'http://ipv4.download.thinkbroadband.com/50MB.zip'
size = requests.get(url, stream=True).headers['Content-length']
print(f'{size} Bytes == 50 Megabytes')
Output:
52428800 Bytes == 50 Megabytes
from ftplib import FTP

ftp = FTP('ftp.server')
ftp.login()
# ftp.size() returns file size in bytes
print(ftp.size('applications/data.zip'))