How to make Python check if ftp directory exists?

前端 未结 8 1825
一生所求
一生所求 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:51
    from ftplib import FTP
    
    ftp = FTP()
    ftp.connect(hostname, 21) 
    ftp.login(username,password)
    
    try:
        ftp.cwd('your folder name')
        #do the code for successfull cd
    except Exception:
        #do the code for folder not exists
    
    0 讨论(0)
  • 2021-02-12 20:53

    Nslt will list an array for all files in ftp server. Just check if your folder name is there.

    from ftplib import FTP 
    ftp = FTP('yourserver')
    ftp.login('username', 'password')
    
    folderName = 'yourFolderName'
    if folderName in ftp.nlst():
        #do needed task 
    
    0 讨论(0)
提交回复
热议问题