Python Forum
FTP not downloading files but showing success - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: FTP not downloading files but showing success (/thread-1783.html)



FTP not downloading files but showing success - python_lover - Jan-25-2017

Hi,

I am new to python.....but have good Unix background.

I was trying to have ftp download a file from remote server to my local unix server.....

import ftplib
ftp = ftplib.FTP(some remote server)
ftp.login(uid,pass)
ftp.pwd()
ftp.cwd('/var/tmp')
ftp.dir()

ftp.retrlines('RETR somefile')

Now what is happening is, I am getting all the messages for dir listing, path etc... and finally am getting "226 file transfer successful".

But the directory where I ran the above code when checked, I didnt find the file downloaded there. Not sure what I have missed.

When I tried to check similar code on the net, found that some guys are opening a local file in write mode and downloading the contents of remote file in it. I find this unnecessary as why cant we just get the file in its entirety as we do in unix ftp session. Why should I be required to create a file at local server in write mode and then dump contents from remote file in it?

Please let me know if I have understood this correctly and please help.

Thanks,
PL


RE: FTP not downloading files but showing success - buran - Jan-25-2017

looking at docs, I think you do need to open a local file to write (i.e. the callback).

There is example with ftp.retrbinary
and for retrline the default callback is stdout

Quote:FTP.retrbinary(command, callback[, maxblocksize[, rest]])

   Retrieve a file in binary transfer mode. command should be an appropriate RETR command: 'RETR filename'. The callback function is called for each block of data received, with a single string argument giving the data block. The optional maxblocksize argument specifies the maximum chunk size to read on the low-level socket object created to do the actual transfer (which will also be the largest size of the data blocks passed to callback). A reasonable default is chosen. rest means the same thing as in the transfercmd() method.

FTP.retrlines(command[, callback])

   Retrieve a file or directory listing in ASCII transfer mode. command should be an appropriate RETR command (see retrbinary()) or a command such as LIST, NLST or MLSD (usually just the string 'LIST'). LIST retrieves a list of files and information about those files. NLST retrieves a list of file names. On some servers, MLSD retrieves a machine readable list of files and information about those files. The callback function is called for each line with a string argument containing the line with the trailing CRLF stripped. The default callback prints the line to sys.stdout.



RE: FTP not downloading files but showing success - python_lover - Jan-25-2017

Hmm thought so, Thanks dude!