Martin Kelly wrote:
> I got it working, but it's rather messy, and I was wondering if there's
> a more elegant way to do it; after all, it is python :).
That's basically the right approach. Three comments.
First, change this...
newfile = file.replace(word , r_word)
os.rename(os.path.join(root, file), os.path.join(root, newfile))
if file != newfile:
print file, "renamed to", newfile
to this...
newfile = file.replace(word , r_word)
if file != newfile:
os.rename(os.path.join(root, file), os.path.join(root, newfile))
print file, "renamed to", newfile
Why? (a) less work for the extremely common case, (b) rename
will update the file's inode-changed time.
Second, instead of using os.getcwd() as your default topdir, you can
use '.', which always resolves to the current directory. Also,
instead of prepending root to each path, you can chdir to the topdir
and use relative paths from there.
if len(sys.argv[1:]) == 3:
top, word, r_word = sys.argv[1:]
os.chdir(sys.argv[1])
elif len(sys.argv[1:]) == 2:
word, r_word = sys.argv[1:]
else:
usage()
and...
for root, dir, files in os.walk('.'):
That'll change your progress message text.
Third, and most important, if you're teaching yourself Python, why not
join EUGLUG's Python mailing list? It's much lower volume than the
mailing list, and it's all about Python.
http://www.euglug.org/mailman/listinfo/python
--
Bob Miller K<bob>
[EMAIL PROTECTED]
_______________________________________________
EUGLUG mailing list
[email protected]
http://www.euglug.org/mailman/listinfo/euglug