python delete directory recursively code example

Example 1: delete folder and its subfolders in python

import shutil
shutil.rmtree('/path/to/your/dir/')

Example 2: how to delete directory in unix

rmdir directoryname  	// This is will delete an empty directory. 

rmdir -r directoryname 	// This is will delete everything in the directory
						// including all files and subdirectories.

Example 3: bash count files in directory recursively matchingattern

ls -Uba1 | grep ^log | wc -l

Example 4: how to scan directory recursively python

import os
import sys

rootdir = sys.argv[1]

for root, subFolders, files in os.walk(rootdir):

    for folder in subFolders:
        outfileName = rootdir + "/" + folder + "/py-outfile.txt" # hardcoded path
        folderOut = open( outfileName, 'w' )
        print "outfileName is " + outfileName

        for file in files:
            filePath = rootdir + '/' + file
            f = open( filePath, 'r' )
            toWrite = f.read()
            print "Writing '" + toWrite + "' to" + filePath
            folderOut.write( toWrite )
            f.close()

        folderOut.close()