Newbie Question: Shell-like Scripting in Python?

Mark McEahern marklists at mceahern.com
Tue Oct 1 23:18:20 EDT 2002


[Scott Meyers]
> So here's my question: will Python let me do that?

Yes.

> I'm not asking about how
> powerful the language is -- I know I can do anything with it.  But suppose
> I want to rename files matching foo.* to be bar.*.  Ideally, I'd
> like to be
> able to say something like this:
>
>   rename foo.* bar.*

import glob
import os
for oldfile in glob.glob("foo.*"):
    newfile = oldfile.replace("foo", "bar")
    os.rename(oldfile, newfile)

// m





More information about the Python-list mailing list