list from FTP server to a text file

Dan M catdude at gmail.com
Thu Jan 6 11:06:12 EST 2011


On Thu, 06 Jan 2011 10:51:42 -0500, Ahmed, Shakir wrote:

> Hi,
> 
> I am trying to create a list in a txt file from an ftp server. The
> following code is retrieving the list of the files but could not able to
> write in a text file. Any help is highly appreciated.
> 
> Thanks
> 
> 
> 
> ****************************
> import os
> import time
> from ftplib import FTP
> ftp = FTP("*.org","","")   # connect to host, default port ftp.login()
> ftp.cwd("/pub/remotefolder/")
> ftp.retrlines('NLST')
> ******************************

WARNING: I am a newbie! Expect more pythonic ways to do this in other 
replies

from ftplib import FTP
ftp = FTP("host", "user", "pass")
ftp.cwd("/pub/myfolder")
files = ftp.nlst(".")
f = open('files.txt', 'w')
for file in files:
	f.write('%s\n' % (file,))
f.close()




More information about the Python-list mailing list