Rename Files with Python

Simple wee python script for renaming files in a directory.

I had a bunch of mp4 files that had a reference code at the end rather than the front so renaming to put the reference code at the front would keep them in order e.g. myfile-m6-03.mp4 to become 6-03-myfile-m6-03.mp4

 #!/usr/bin/env python
from os import rename
from glob import glob

for file in glob('*.mp4'):
extension = file[-8:]
mod_code = extension[:4]
new_file_name = "%s-%s" % (mod_code, file)
print("Moving %s to %s" % (file, new_file_name))
rename(file, new_file_name)

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>