The Size() Method Of FTP Class In Python

Method Name:

size

Method Signature:

size(filename)

Parameters:

filename:Name of the file

Return Value:

  • Upon success, returns the file size in number of bytes.
  • Upon failure, it returns None.

Overview:

  • The method size(), returns the size of the file in number of bytes.
  • The size() method, internally sends the FTP command SIZE appended by a space and the filename.
  • Upon success, the server sends back a 213 message, with the size of the file in number of bytes, which is parsed and returned by the size() method.
  • The size() method works for files; it does not work for directories.

Example:

# Example Python Program to get the size of a file in the FTP server

from ftplib import FTP

 

# Connect to the host

ftp = FTP(host="speedtest.tele2.net");

print(ftp.getwelcome());

 

# Login to the FTP server

loginRepsonse = ftp.login();

print(loginRepsonse);

 

# Get the size of a file

fileName = "1KB.zip";

size = ftp.size(fileName);

print("Size of the file %s is %s bytes"%(fileName, size));

 

Output:

220 (vsFTPd 3.0.3)

230 Login successful.

Size of the file 1KB.zip is 1024 bytes


Copyright 2022 © pythontic.com