Skip to content

Instantly share code, notes, and snippets.

@keithio
Created August 21, 2011 03:16
Show Gist options
  • Save keithio/1160058 to your computer and use it in GitHub Desktop.
Save keithio/1160058 to your computer and use it in GitHub Desktop.
Traverse a directory structure and print contents with indentation
'''
Copyright (c) 2011, Keith Hall <http://keith.io>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import pprint
import os
import sys
def custom_listdir(path):
"""
Returns the content of a directory by showing directories first and then
files in alphabetical order
"""
dirs = sorted([d for d in os.listdir(path) if os.path.isdir(path + os.path.sep + d)])
dirs.extend(sorted([f for f in os.listdir(path) if os.path.isfile(path + os.path.sep + f)]))
return dirs
def generate(path, level, f):
"""
Returns a list of directories and files for the provided path
"""
# base directory
dirdata = []
# loop over items in directory
for item in custom_listdir(path):
itempath = os.path.join(path, item)
itempathbase = os.path.basename(itempath)
# skip hidden directories
if itempathbase[0] == '.' or itempath == '..':
continue
# append directory to list
if os.path.isdir(itempath):
f.write(' '*level + itempathbase + '/' + "\n")
# get next level data
generate(itempath, level+1, f)
# append file to list
if os.path.isfile(itempath):
f.write(' '*level + item + "\n")
return dirdata
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit('Usage: printdircon.py DIRECTORY OUTFILE\nExample: printdircon.py /home/username /home/username/output.txt')
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: Directory %s does not exist!' % sys.argv[1])
rootdir = sys.argv[1]
outfile = sys.argv[2]
data = [rootdir]
# open file
f = open(outfile, 'w')
folder = os.path.abspath(rootdir)
f.write("Directory contents for: %s\n" % folder)
f.write("========================" + '='*len(folder) + "\n\n")
data = generate(rootdir, level=0, f=f)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment