Rename files recursivly

From wiki.network-crawler.de
Jump to: navigation, search

Rename files with python

#!/usr/bin/env python

import sys
import subprocess
import os
import stat

searchpath = "/home/ftpuser"

def walktree (top = searchpath, depthfirst = True):
    names = os.listdir(top)
    if not depthfirst:
        yield top, names
    for name in names:
        try:
            st = os.lstat(os.path.join(top, name))
        except os.error:
            continue
        if stat.S_ISDIR(st.st_mode):
            for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
                yield newtop, children
    if depthfirst:
        yield top, names

for (basepath, children) in walktree(searchpath,False):
    for child in children:
	str = os.path.join(basepath, child)
	if str.find("canopen") <> -1:
		if str.find("3S Laufzeitsysteme") <> -1:
			cmd = "mv " + basepath + "/CANopen\ f*r\ 3S\ Laufzeitsysteme\ V2_3_5_0.pdf " + basepath + "/CANopen\ fuer\ 3S\ Laufzeitsysteme\ V2_3_5_0.pdf"
			print cmd
			subprocess.call(cmd, shell=True)