Created
April 15, 2017 09:56
-
-
Save autosquid/538dbdfca0480118b29c10372408909c to your computer and use it in GitHub Desktop.
remove '_' in basename: 1_.2.3 -> 1.2.3
This file contains hidden or 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 os | |
import shutil | |
import click | |
@click.group() | |
def cli(): | |
pass | |
def isfmt(f): | |
parts = f.split('.') | |
if len(parts) != 3: | |
return False | |
return parts[0].endswith('_') | |
def refmt (f): | |
parts = f.split('.') | |
return '.'.join([parts[0][:-1],]+ parts[1:]) | |
@cli.command() | |
@click.argument("srcdir", type=click.Path()) | |
def rdname(srcdir): | |
if os.path.isabs(srcdir) is False: | |
srcdir = os.path.join(os.path.abspath('.'), srcdir) | |
print srcdir | |
fnames = filter(isfmt, os.listdir(srcdir)) | |
nnames = map(refmt, fnames) | |
for f, n in zip(fnames, nnames): | |
print '{} --> {}'.format(f,n) | |
shutil.move(os.path.join(srcdir, f), os.path.join(srcdir, n)) | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment