Entry
How can I store the result of ftplib.FTP.dir() function in a variable?
Nov 7th, 2005 13:06
Brian Okken, Nelsie Marabe,
Here is at least one way to grab the output of dir() in a list.
Redirect stdout before calling it.
Sample code:
import sys
import ftplib
dirList = []
class CollectDirOutput:
def write(self,string):
if len(string) > 1:
dirList.append(string)
ftp = ftplib.FTP('host','login','password')
ftp.cwd('dirYouCareAbout')
oldout = sys.stdout
sys.stdout = CollectDirOutput()
ftp.dir()
sys.stdout = oldout
ftp.close
for d in dirList:
print d