XML File Listing

Written: 01/07/15

Last Updated: 01/07/15

A while back I made a very basic Python program to print out the contents in a directory. The program is designed to create a full (recursive) directory listing of all of the contents within a provided path or set of paths. The details are created in a properly indented XML file. I decided to go ahead and publish the code. It’s licensed under the MIT license and is available here.

Note that this code was designed to create small directory listings. Python’s default routines for tree traversal and obtaining meta data are very expensive. As such, creating the listings for large trees may take quite some time. For a more robust solution it is recommended to use the appropriate OS hooks for accessing the files.

The XML has the following tags: name, directory, file, size, created_time, modified_time, accessed_time. The first line of the XML will always consist of a name element and refer to the outermost directory. From then on, all other name elements must be inside a directory or file element. Using this convention, one could build the full path to any directory or file element by simply concatenating all of the parent names.

Each directory element consists of a name and zero or more file elements. Each file element consists of at least a name. If it was desired to store the size, created time, modified time, or accessed time, those elements will also be within the file element.

I provided two examples for your convenience. This example shows the output for the “C:\Windows\Help” directory. Using XMLViewer the output looks like the following:

c_windows_help

The second example shows the output for the “C:\Python27” directory. It looks something like the following:

c_python27

Details on how to use the code, as well as the full source, are available on my GitHub.

Leave a Reply