Last active
February 5, 2018 08:42
-
-
Save louisswarren/9e2281df7fe559e6ccbffb07bc307d03 to your computer and use it in GitHub Desktop.
Batch rename files using your editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.write(('\n'.join(old_names)).encode()) | |
fname_file.flush() | |
subprocess.call(EDITOR.split() + [fname_file.name]) | |
fname_file.seek(0) | |
new_contents = fname_file.read() | |
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: | |
main(sys.argv[1:]) | |
else: | |
main(os.listdir()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you leave lines blank, silly things can happen. GIGO.