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.
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).
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
dictand you’ll be able to do things likeroot['etc']['ssh']['config'].If you just need a simple tree structure, have you considered just using nested
dicts?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
/etcis justroot['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 🙂