Skip to content

Instantly share code, notes, and snippets.



Last active Feb 5, 2018
What would you like to do?
Batch rename files using your editor
#!/usr/bin/env python3
import os
import subprocess
import tempfile
EDITOR = os.environ.get('EDITOR', 'vim')
def input_new_names(old_names):
with tempfile.NamedTemporaryFile(suffix='.tmp') as fname_file:
fname_file.flush() + [])
new_contents =
return new_contents.decode().strip().split('\n')
def main(args):
targets = sorted(args)
destinations = input_new_names(targets)
for old, new in zip(targets, destinations):
os.rename(old, new)
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:

This comment has been minimized.

Copy link
Owner Author

@louisswarren louisswarren commented Feb 5, 2018

If you leave lines blank, silly things can happen. GIGO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.