faqts : Computers : Programming : Languages : Python : Modules : ftplib

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 7 people (100%) answered Yes
Recently 7 of 7 people (100%) answered Yes

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