nlst() with file/directory detection example : ftp « Network « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Network » ftp 
21.24.12.nlst() with file/directory detection example
import ftplib

class DirEntry:
    def __init__(self, filename, ftpobj, startingdir = None):
        self.filename = filename
        if startingdir == None:
            startingdir = ftpobj.pwd()
        try:
            ftpobj.cwd(filename)
            self.filetype = 'd'
            ftpobj.cwd(startingdir)
        except ftplib.error_perm:
            self.filetype = '-'
        
    def gettype(self):
        return self.filetype

    def getfilename(self):
        return self.filename

f = ftplib.FTP('ftp.kernel.org')
f.login()

f.cwd('/pub/linux/kernel')
nitems = f.nlst()
items = [DirEntry(item, f, f.pwd()) for item in nitems]

print "%d entries:" % len(items)
for item in items:
    print "%s: type %s" (item.getfilename(), item.gettype())
f.quit()
21.24.ftp
21.24.1.Using Python to Fetch Files from an FTP Server
21.24.2.Interactive FTP Example
21.24.3.FTP Download Example
21.24.4.Binary file download
21.24.5.ASCII file download
21.24.6.Binary file upload
21.24.7.Binary file download 2
21.24.8.Basic FTP connection
21.24.9.File dir() example
21.24.10.Parse return value from dir() function
21.24.11.FTP nlst example
21.24.12.nlst() with file/directory detection example
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.