else/Toolbox/brename.py

77 lines
1.9 KiB
Python
Raw Normal View History

2016-12-05 01:50:52 +00:00
'''
Batch rename files by providing a string to be `eval`ed, using variable `x` as
the current filename.
Yes I know this is weird, but for certain tasks it's just too quick and easy to pass up.
For example:
Prefix all the files:
brename.py "'Test_' + x"
Keep the first word and extension:
brename.py "(x.split(' ')[0] + '.' + x.split('.')[-1]) if ' ' in x else x"
'''
2016-12-02 06:37:07 +00:00
import os
2016-12-05 01:50:52 +00:00
import random
import re
2016-12-02 06:37:07 +00:00
import sys
2016-12-23 03:42:21 +00:00
from voussoirkit import safeprint
2016-12-02 06:37:07 +00:00
def brename(transformation):
old = os.listdir()
new = [eval(transformation) for x in old]
pairs = []
for (x, y) in zip(old, new):
if x == y:
continue
pairs.append((x, y))
if not loop(pairs, dry=True):
print('Nothing to replace')
return
print('Is this correct? y/n')
if input('>').lower() not in ('y', 'yes', 'yeehaw'):
return
loop(pairs, dry=False)
def longest_length(li):
longest = 0
for item in li:
longest = max(longest, len(item))
return longest
def loop(pairs, dry=False):
has_content = False
for (x, y) in pairs:
if dry:
line = '{old}\n{new}\n'
line = line.format(old=x, new=y)
#print(line.encode('utf-8'))
2016-12-23 03:42:21 +00:00
safeprint.safeprint(line)
2016-12-02 06:37:07 +00:00
has_content = True
else:
os.rename(x, y)
return has_content
def title(text):
(text, extension) = os.path.splitext(text)
text = text.title()
if ' ' in text:
(first, rest) = text.split(' ', 1)
else:
(first, rest) = (text, '')
rest = ' %s ' % rest
for article in ['The', 'A', 'An', 'At', 'To', 'In', 'Of', 'From', 'And']:
article = ' %s ' % article
rest = rest.replace(article, article.lower())
rest = rest.strip()
if rest != '':
rest = ' ' + rest
text = first + rest + extension
return text
if __name__ == '__main__':
transformation = sys.argv[1]
2016-12-05 01:50:52 +00:00
brename(transformation)