In parts 1 and 2 I discussed code to use Python to recursively walk a directory and emit a nested XML representation of the contents.
Dave Pawson built on my basic techniques and came up with dirlist.py
, a fully tricked-out version with all sorts of options and amenities. Well, he wasn't even finished. He sent me a further version today in which he "tidied up [the] program, and added options [for file] date and size."
Cool. I've posted it here: dirlist2.py. If further versions are toward, I'll move it into my CVS. Dave is a self-confessed Python newbie. I had to make some quick fixes just to get it to work on my machine, but I haven't had time to carefully vet the entire program. Please let us know if you run into trouble (a comment here should suffice).
Usage example:
$ mkdir foo $ mkdir foo/bar $ touch foo/a.txt $ touch foo/b.txt $ touch foo/bar/c.txt $ touch foo/bar/d.txt $ python dirlist2.py foo/ Processing /home/uogbuji/foo <?xml version="1.0" encoding="UTF-8"?> <directory name="/home/uogbuji/foo"> <file name="a.txt"/> <file name="b.txt"/> <directory name="/home/uogbuji/foo/bar"> <file name="c.txt"/> <file name="d.txt"/> </directory> </directory> $ python dirlist2.py -d foo Adding file dates Processing /home/uogbuji/foo <?xml version="1.0" encoding="UTF-8"?> <directory name="/home/uogbuji/foo"> <file date="2005-05-09" name="a.txt"/> <file date="2005-05-09" name="b.txt"/> <directory name="/home/uogbuji/foo/bar"> <file date="2005-05-09" name="c.txt"/> <file date="2005-05-09" name="d.txt"/> </directory> </directory> $ python dirlist2.py foo/ foo.xml Processing /home/uogbuji/foo $ cat foo.xml <?xml version="1.0" encoding="UTF-8"?> <directory name="/home/uogbuji/foo"> <file name="a.txt"/> <file name="b.txt"/> <directory name="/home/uogbuji/foo/bar"> <file name="c.txt"/> <file name="d.txt"/> </directory> </directory>