Skip to content

Instantly share code, notes, and snippets.

@LenzGr LenzGr/
Created Jan 6, 2015

What would you like to do?
Recursively add a prefix to all files and directories
import os, sys
def walktree(top, callback):
for path, dirs, files in os.walk(top, topdown=False):
for filename in files:
if not filename.startswith("."):
fullPath = os.path.join(path, filename)
for directory in dirs:
if not directory.startswith("."):
fullPath = os.path.join(path, directory)
def renameFile(file):
dirName, fileName = os.path.split(file)
newName = dirName + os.sep + fileprefix + fileName
print "Renaming " + file + " to " + newName
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('Usage: %s <directory> <prefix>' % sys.argv[0])
directory = sys.argv[1]
fileprefix = sys.argv[2]
if not os.path.exists(directory):
sys.exit('ERROR: Directory %s was not found!' % directory)
print "directory: " + sys.argv[1]
print "prefix: " + fileprefix
walktree(directory, renameFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.