os.path.walk oddity
Simon Callan
simon at callan.demon.co.uk
Wed Feb 14 18:54:02 EST 2001
For reasons to complicated to go into here, I am having to rearrange a
source tree. Basically every .c file has to be moved into a
subdirectory called 'c', with the extension removed, while .h files
are moved to a 'h' directory. I.e
fred/jim.c -> fred.c.jim
fred/sheila.h -> fred.h.sheila
After figuring out how to stop os.path.walk crashing due to renaming
files, I ended up with the following:
=====
#!/usr/bin/env python
import os
def rename(dir, root, ext):
new_dir = os.path.join(dir, ext)
if not os.path.exists(new_dir):
os.mkdir(new_dir)
o_file = os.path.join(dir, root) + '.' + ext
n_file = os.path.join(dir, ext, root)
print "%s -> %s" % (o_file, n_file)
os.rename(o_file, n_file)
def toRiscos(arg, dir, names):
temp = names
count = 0
for file in temp:
root, ext = os.path.splitext(file)
if ext == '.c':
del names[count]
rename(dir, root, 'c')
elif ext == '.h':
del names[count]
rename(dir, root, 'h')
count += 1
os.path.walk('.', toRiscos, None)
=====
However, there is one problem. If I run the above on the python 2.0
source directory, it takes 3 runs of the programme to completely
convert the tree.
Can anyone explain why this is?
Simon
--
http://www.callan.demon.co.uk/simon/
More information about the Python-list
mailing list