Determine if a listing is a directory or file in Python over FTP

后端 未结 3 791
遥遥无期
遥遥无期 2020-12-19 08:40

Python has a standard library module ftplib to run FTP communications. It has two means of getting a listing of directory contents. One, FTP.nlst()

3条回答
  •  天命终不由人
    2020-12-19 09:00

    Unfortunately FTP doesn't have a command to list just folders so parsing the results you get from ftp.dir() would be 'best'.

    A simple app assuming a standard result from ls (not a windows ftp)

    from ftplib import FTP
    
    ftp = FTP(host, user, passwd)
    for r in ftp.dir():
        if r.upper().startswith('D'):
            print r[58:]  # Starting point
    

    Standard FTP Commands

    Custom FTP Commands

提交回复
热议问题