Created
September 2, 2013 22:02
-
-
Save Perlence/6417653 to your computer and use it in GitHub Desktop.
Open filenames in the text editor for further changes
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
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