Skip to content

Instantly share code, notes, and snippets.

@Perlence
Created September 2, 2013 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Perlence/6417653 to your computer and use it in GitHub Desktop.
Save Perlence/6417653 to your computer and use it in GitHub Desktop.
Open filenames in the text editor for further changes
import sys
import os
import glob
import subprocess
import tempfile
EDITOR = 'c:/Program Files/Sublime Text 2/sublime_text.exe'
files = sys.argv[1:]
if not files:
sys.exit('No files supplied')
# Run glob for each argument
files = reduce(lambda a, b: a + glob.glob(b), files, [])
# Create temp file and write filenames into it
with tempfile.NamedTemporaryFile(mode='w', delete=False) as fp:
fp.write('\n'.join(files))
# Run EDITOR for temp file
subprocess.Popen([EDITOR, fp.name])
# Wait for user prompt
try:
raw_input('Press ENTER when ready ')
except KeyboardInterrupt:
sys.exit('')
# Read contents of the file
with open(fp.name) as fp:
newfiles = fp.read().splitlines()
os.unlink(fp.name)
# Rename files
for old, new in zip(files, newfiles):
os.rename(old, new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment