Using Python's ftplib to get a directory listing, portably

前端 未结 7 648
盖世英雄少女心
盖世英雄少女心 2020-12-04 16:37

You can use ftplib for full FTP support in Python. However the preferred way of getting a directory listing is:

# File: ftplib-example-1.py

import ftplib

f         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 17:08

    Try to use ftp.nlst(dir).

    However, note that if the folder is empty, it might throw an error:

    files = []
    
    try:
        files = ftp.nlst()
    except ftplib.error_perm, resp:
        if str(resp) == "550 No files found":
            print "No files in this directory"
        else:
            raise
    
    for f in files:
        print f
    

提交回复
热议问题