Skip to content Skip to sidebar Skip to footer

How To Store A Directory Structure From An Ftp Site?

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 t

Solution 1:

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'].

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': {
    }
}

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().

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 :)

Solution 2:

For the records, an alternative is

list(os.walk(path))

Post a Comment for "How To Store A Directory Structure From An Ftp Site?"