如何从ftp站点存储目录结构? [英] How to store a directory structure from an ftp site?

查看:134
本文介绍了如何从ftp站点存储目录结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ftp站点读取文件系统目录结构,以便以后可以查找特定文件进行下载(在不同时间或根本不下载).

I need to read a filesystem directory structure from an ftp site, so that later I can seek out specific files for downloading (at different times or not at all).

下载目录结构时,我正在使用以下

when downloading the directory structure I am using the following

class remoteFileSystem:
    directory_structure = ?
    def parse_directory_listing(self,listing_str):
            print listing_str

    def readFileListingFTP(self,target):
            ftpaddress = target.ip_address
            ftp_serv = ftplib.FTP(ftpaddress)
            ftp_serv.login('root', 'pass')
            response = ftpserv.retrlines('LIST',parse_directory_listing)

在回调还没有执行任何操作的情况下,该类还没有用于存储目录结构的特定成员.

Where the callback doesn't do anything yet, and the class has no particular members yet for storing the directory structure.

是否存在将目录列表粘贴到xml或本机目录结构类型对象中的好方法?也就是说,是否存在一些我不知道的东西可以使我免去自己编写东西的工作(我承认没有太多的编码,但是我一直在寻找更多的Python方式来做事情).

Is there a nice pythonic way of sticking the directory listings into a xml or native directory structure type object? ie does something exist that I am not aware of that will save me from rolling my own stuff (not much coding I admit, but I am always looking for more pythonic ways of doing stuff).

推荐答案

我不知道本机数据类型.注意,目录结构是一个非常简单的树形结构.如果您不仅需要简单的树状列表,建议您滚动自己的Directory和File对象.允许您的Directory对象充当dict的角色,您将可以执行root['etc']['ssh']['config']之类的事情.

No native datatype that I'm aware of. Note that a directory structure is a pretty simple tree structure though. If you need more than just a simple tree listing, I'd recommend rolling your own Directory and File objects. Allow your Directory object to act like a dict and you'll be able to do things like root['etc']['ssh']['config'].

如果只需要简单的树结构,是否考虑过使用嵌套的dict s?

If you just need a simple tree structure, have you considered just using nested dicts?

root = {
    'etc': {
        'passwd': None, 
        'shadow': None, 
        'ssh': {
            'config': None}
    }, 
    'lib': {
    }
}

我将无"用作叶节点的数据值,但是如果愿意,您当然可以在其中存储文件元数据.导航树结构非常简单. /etc的列表只是root['etc'].keys().

I'm using None as the data value for a leaf node, but you could certainly store file metadata there instead if you like. Navigating the tree structure is very simple. A listing of /etc is just root['etc'].keys().

最后,宠爱时间. 我一直在寻找更多的Python方式来做事"这是什么意思?寻找想法/建议/指导是完全合理的,但最终,拥有有效代码的人胜出.在这种情况下,"Python的做事方式"就是做到这一点.在Python中,当然:)

Finally, pet peeve time. "I am always looking for more pythonic ways of doing stuff" What does that mean? It's perfectly reasonable to look for ideas/advice/guidance, but at the end of the day, the guy with the working code wins. In this case, the "pythonic way of doing stuff" is to just do it. In Python, of course :)

这篇关于如何从ftp站点存储目录结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆