python iterate over directory recursively code example
Example 1: python loop through files in directory recursively
import os
rootdir = './path/to/files/'
for subdir, dirs, files in os.walk(rootdir):
for file in files:
print os.path.join(subdir, file)
Example 2: 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"
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()