Monday, December 10, 2012

[Python] RecurseMyDir.py - Recursive directory listing

This program uses the walk() function of the Python OS module/namespace to recursively search directories starting with the supplied root directory, and then prints the sub-directories and their contents.


#!/usr/bin/python

##################################

#       RecurseMyDir.py

#

#This program recursively lists

#all files and directories in

#supplied directory.

#

#By: Jacob Holcomb/Gimppy

#Date: 12/09/2012

##################################

import os, time

def isDir():

    Dir = ""

    while not os.path.isdir(Dir):

        time.sleep(.5)

        Dir = raw_input("\n[*] Please enter a directory: ")

        if os.path.isfile(Dir):

            print "\n[!!!] You entered %s which is a file path. Please enter an absolute directory! [!!!]\n" % Dir

        elif os.path.isdir(Dir):

            print "\n[*] You entered %s as your directory.\n" % Dir

            time.sleep(1)

        else:

            print "\n[!!!] You entered %s. Please enter an absolute directory! [!!!]\n" % Dir

    return Dir

def listdir(Dir):

    rootList = []

    files = []

    directory = []

    rootList = os.listdir(str(Dir))

    print "\n\n[*] Directory %s contained %d files and subdirectories [*]\n\n\n    ----Files and Subdirectories: %s\n\n\n[!!!] Now printing recursive directory listing starting at %s [!!!]\n" % (Dir,len(rootList),rootList,os.path.abspath(Dir))

    time.sleep(2)

    try:

     

        for rootOfDir, dirName, files in os.walk(Dir):

            for dir in dirName:

                directory.append(os.path.join(rootOfDir,dir))

                for dir in directory:

                    files = os.listdir(dir)

                 

                print "\n[*] Directory: %s\n" % dir

                print "\t----Files\n\n"

                for file in files:

                    results = "\t\t----Name: " + str(file) + "\n"

                 

                    print results

                        

    except:

        print "\n\n[!!!] Error processing and printing your results. [!!!]\n\n"

     

     

def main():

    banner = "##################################\n"
    banner +="#       RecurseMyDir.py           \n"
    banner +="#                                 \n"
    banner +="#This program recursively lists   \n"
    banner +="#all files and directories in     \n"
    banner +="#supplied directory.              \n"
    banner +="#                                 \n"
    banner +="#By: Jacob Holcomb/Gimppy         \n"
    banner +="#Date: 12/09/2012                 \n"
    banner +="##################################\n"

    print banner

    contin = ""

    while contin != "yes" and contin != "no":

        time.sleep(.5)

        contin = raw_input("[*] Would you like to continue? Please enter yes or no.\n\n> ")

        if contin == "yes":

            listdir(isDir())

        elif contin == "no":

            print "\n[!!!] You are now exiting... [!!!]\n"

        else:

            print "\n[!!!] You entered %s. Please respond with yes or no! [!!!]\n" % contin

#Top-level script environment

if __name__ == "__main__":

    main()
)

No comments:

Post a Comment