How to make Python check if ftp directory exists?

前端 未结 8 1839
一生所求
一生所求 2021-02-12 20:17

I\'m using this script to connect to sample ftp server and list available directories:

from ftplib import FTP
ftp = FTP(\'ftp.cwi.nl\')   # connect to host, defa         


        
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 20:46

    you can use a list. example

    import ftplib
    server="localhost"
    user="user"
    password="test@email.com"
    try:
        ftp = ftplib.FTP(server)    
        ftp.login(user,password)
    except Exception,e:
        print e
    else:    
        filelist = [] #to store all files
        ftp.retrlines('LIST',filelist.append)    # append to list  
        f=0
        for f in filelist:
            if "public_html" in f:
                #do something
                f=1
        if f==0:
            print "No public_html"
            #do your processing here
    

提交回复
热议问题