Is there a way to download only partial piece of file from last line (end of file). Like if file is over 40 MB, and I would like to retrieve only last block let's say of 2042 bytes. Is there possible way to do this using python 3 with ftplib ?
-
No, as far as I'm aware, it isn't possible to do this due to way we download and receive packets. However, you can stream the file and then just read the last few chunks. Refer to [this answer](https://stackoverflow.com/a/18773444/8381175) for more details. – N M Oct 13 '17 at 10:37
2 Answers
Try using the FTP.retrbinary()
method and supply the rest
argument, which is an offset into the requested file. Since the offset is from the beginning of the file, you will need to calculate the offset using the size of the file and the desired number of bytes of data. Here's an example using debian's FTP server:
from ftplib import FTP
hostname = 'ftp.debian.org'
filename = 'README'
num_bytes = 500 # how many bytes to retrieve from end of file
ftp = FTP(hostname)
ftp.login()
ftp.cwd('debian')
cmd = 'RETR {}'.format(filename)
offset = max(ftp.size(filename) - num_bytes, 0)
ftp.retrbinary(cmd, open(filename, 'wb').write, rest=offset)
ftp.quit()
This will retrieve the last num_bytes
bytes from the end of the requested file and write it to a file of the same name in the current directory.
The second argument to retrbinary()
is a callback function, in this case it's the write()
method of a writeable file. You can write your own callback to process the retrieved data.

- 84,695
- 9
- 117
- 138
Just use the rest
argument to retrbinary
to tell the server at which file offset it should start to transfer data. From the documentation:
FTP.retrbinary(command, callback[, maxblocksize[, rest]])
... rest means the same thing as in the transfercmd() method.FTP.transfercmd(cmd[, rest])
... If optional rest is given, a REST command is sent to the server, passing rest as an argument. rest is usually a byte offset into the requested file, telling the server to restart sending the file’s bytes at the requested offset, skipping over the initial bytes.

- 114,247
- 10
- 131
- 172