zebra
zebra

Reputation: 6531

How to batch rename in windows with python

I have a large list of file names that are illegal, I want to rename them with the 'ren' command. How can I execute 'ren' in batch on windows 7 with python?

Upvotes: 2

Views: 439

Answers (3)

wd5
wd5

Reputation: 53

sts = os.system("mycmd" + " myarg")

or

sts = call("mycmd" + " myarg", shell=True)

http://docs.python.org/library/subprocess.html

example:


    import os
    os.system("rename.bat")
    #or use command "start" for start bat files

Upvotes: 0

Li-aung Yip
Li-aung Yip

Reputation: 12486

Are you sure you don't really want to use a batch renaming utility?

Don't reinvent the wheel.

Upvotes: 0

tibur
tibur

Reputation: 11636

You should use the python function, it will be easier to catch errors rather than relying on the ren command.

import os
os.rename(src, dst)

Upvotes: 2

Related Questions