reverseMove script
This commit is contained in:
1 parent
ece9132ca1
commit
84bf2277de
1 file changed
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
def parse_line(line):
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
return None
|
||||
|
||||
for sep in ["→", "->"]:
|
||||
if sep in line:
|
||||
src, dst = map(str.strip, line.split(sep, 1))
|
||||
return src, dst
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def move_with_overwrite(src, dst):
|
||||
if not os.path.exists(src) and not os.path.islink(src):
|
||||
print(f"[SKIP missing source] {src}")
|
||||
return
|
||||
|
||||
# If destination exists, remove it (overwrite behavior)
|
||||
if os.path.lexists(dst):
|
||||
try:
|
||||
if os.path.isdir(dst) and not os.path.islink(dst):
|
||||
shutil.rmtree(dst)
|
||||
else:
|
||||
os.remove(dst)
|
||||
except Exception as e:
|
||||
print(f"[FAIL delete dst] {dst}: {e}")
|
||||
return
|
||||
|
||||
# Ensure parent directory exists
|
||||
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
||||
|
||||
try:
|
||||
shutil.move(src, dst)
|
||||
print(f"[OK] {src} -> {dst}")
|
||||
except Exception as e:
|
||||
print(f"[FAIL move] {src} -> {dst}: {e}")
|
||||
|
||||
|
||||
def process(file_path):
|
||||
with open(file_path, "r") as f:
|
||||
for line in f:
|
||||
parsed = parse_line(line)
|
||||
if not parsed:
|
||||
continue
|
||||
|
||||
dst, src = parsed
|
||||
move_with_overwrite(src, dst)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python rename_apply.py <file>")
|
||||
sys.exit(1)
|
||||
|
||||
process(sys.argv[1])
|
||||
Reference in new issue
Block a user