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

前端 未结 7 644
盖世英雄少女心
盖世英雄少女心 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:00

    That helped me with my code.

    When I tried feltering only a type of files and show them on screen by adding a condition that tests on each line.

    Like this

    elif command == 'ls':
        print("directory of ", ftp.pwd())
        data = []
        ftp.dir(data.append)
    
        for line in data:
            x = line.split(".")
            formats=["gz", "zip", "rar", "tar", "bz2", "xz"]
            if x[-1] in formats:
                print ("-", line)
    

提交回复
热议问题