cmd/inputrename.py

34 lines
831 B
Python
Raw Normal View History

2020-12-08 04:31:10 +00:00
'''
Given a target string to replace, rename files by prompting the user for input.
'''
import argparse
2020-02-28 04:53:00 +00:00
import os
import sys
2020-12-08 04:31:10 +00:00
from voussoirkit import pipeable
2020-10-26 03:13:59 +00:00
2020-12-08 04:31:10 +00:00
@pipeable.ctrlc_return1
2020-10-26 03:13:59 +00:00
def inputrename_argparse(args):
2020-12-08 04:31:10 +00:00
files = (file for file in os.listdir() if args.keyword in file)
2020-10-26 03:13:59 +00:00
prev = None
for file in files:
print(file)
this = input('> ')
if this == '' and prev is not None:
this = prev
if this:
os.rename(file, file.replace(args.keyword, this))
prev = this
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('keyword')
parser.set_defaults(func=inputrename_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
2020-12-08 04:31:10 +00:00
raise SystemExit(main(sys.argv[1:]))