[Tutor] Assigning a variable to an FTP directory listing

Pat Martin wpmartin at gmail.com
Thu Dec 12 16:37:33 CET 2013


Below is what worked for me, thank you. I had a feeling it had to do
with the callback but have never dealt with callbacks before so wasn't
sure. Time to do more reading.

For those asking about alternatives to FTP, its for a vendor who only
uses FTP so no choice in that.

Thanks all for the help.

On Wed, Dec 11, 2013 at 4:22 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Wed, Dec 11, 2013 at 03:55:50PM -0800, Pat Martin wrote:
>> Hello,
>>
>> I am writing a program that needs to pull all of the files from a
>> specific directory. I have a few lines written that give me the list
>> of files but when I try to assign it to a variable the variable ends
>> up equaling "226 Directory send Ok", this is a snippet of my code.
>
> I don't have an FTP server to test this against, but try this instead:
>
> from ftplib import FTP
> ftp = FTP(ftpserver)
> ftp.login(user=username, passwd=password)
> ftp.cwd(remoteworkdir)
> listoffiles = []
> status = ftp.retrlines('NLST', callback=listoffiles.append)
> ftp.quit()
> if status != "226 Directory send OK."
>     # An error?
>     print status
> for filename in listoffiles:
>     print filename
>
>
>
> --
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list