-
Notifications
You must be signed in to change notification settings - Fork 0
/
ren
executable file
·46 lines (35 loc) · 1007 Bytes
/
ren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
import os
import readline
import sys
def input_with_prefill(prompt, text):
def hook():
readline.insert_text(text)
readline.redisplay()
readline.set_pre_input_hook(hook)
if sys.version_info >= (3,):
result = input(prompt)
else:
result = raw_input(prompt)
readline.set_pre_input_hook()
return result
if len(sys.argv) == 1:
raise ValueError("usage ren <FILENAME>")
source = sys.argv[1]
if source == "":
raise ValueError("empty filename")
if not os.path.exists(source):
raise ValueError(source, " file does not exists")
out = input_with_prefill('new name: ', source)
if out == source:
print("same name, do mothing.")
sys.exit(0)
if out == "":
raise ValueError("out filename cannot be empty")
print(f"rename from:[{source}]")
print(f"rename to:[{out}]")
ok = input("rename ? y/n[Y]: ") or "Y"
if ok == "Y":
os.rename(source, out)
else:
print("canceled")