python script - kimdonggwan337/dongdong GitHub Wiki
[replace.py]
#!/usr/local/bin/python3.10
import re
import os
# μμ λ©μμ§
print(" μ¬λ¬ κ°μ ν
μ€νΈ νμΌμ λ³κ²½ν μ μμ΅λλ€.")
print(" μ) /home/user/file1.txt,/home/user/docs/file2.txt")
print("----------------------------------------------------------")
# μ¬μ©μ μ
λ ₯
file_input = input(" μμ ν ν
μ€νΈ νμΌ κ²½λ‘(μ¬λ¬ κ°λ©΄ μΌνλ‘ κ΅¬λΆ): ").strip()
target = input(" μ°Ύμ λ¬Έμ(λλ λ¨μ΄): ").strip()
replacement = input(" λ°κΏ λ¬Έμ(λλ λ¨μ΄): ").strip()
# λ³μ μ μΈ
raw_paths = [p.strip() for p in file_input.split(",") if p.strip()]
pattern = re.compile(re.escape(target), re.IGNORECASE)
# κ²½λ‘ μ ν¨μ± κ²μ¬ λ° μΉν νμ μΈκΈ°
for original in raw_paths:
file_path = os.path.abspath(os.path.expanduser(original))
print(f"\n μ²λ¦¬ μ€: {file_path}")
if not os.path.isfile(file_path):
print(f" μλͺ»λ νμΌμ
λλ€.(κ²½λ‘ λλ λλ ν°λ¦¬): {file_path}")
continue
# μΉν λμ λ¬Έμ νμ μΈκΈ°
try:
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
count = 0
matches = pattern.findall(text)
count = len(matches)
print(f" '{target}'(λμλ¬Έμ 무μ) λ°κ²¬ νμ: {count}κ°")
except Exception as e:
print(f" νμΌ μ½κΈ° μ€ν¨: {file_path} ({e})")
continue
if count == 0:
print(" μΉν λμμ΄ μμ΄ κ²°κ³Ό νμΌμ μμ±νμ§ μμ΅λλ€.")
continue
# μΉν λ° κ²°κ³Ό νμΌ μ μ₯
try:
new_text = pattern.sub(replacement, text)
base, ext = os.path.splitext(file_path)
output_file = f"{base}_replaced{ext}"
with open(output_file, 'w', encoding='utf-8') as out_f:
out_f.write(new_text)
print(f"'{target}' β '{replacement}' μΉν μλ£")
print(f"κ²°κ³Ό νμΌ: {output_file}")
except Exception as e:
print(f" νμΌ μ μ₯ μ€ν¨: {file_path} ({e})")
continue
print("\n λͺ¨λ νμΌ μ²λ¦¬ μλ£!")
[move.py]
#!/usr/local/bin/python3.10
import os, shutil
from datetime import datetime, timedelta
source = input("ν΄λ κ²½λ‘λ₯Ό μ
λ ₯νμΈμ.: ").strip()
destination = input("μ΄λ λμ ν΄λ κ²½λ‘: ").strip()
os.makedirs(destination, exist_ok=True)
if not os.path.isdir(source):
print("ν΄λΉ κ²½λ‘κ° μκ±°λ ν΄λκ° μλλλ€.")
exit(1)
print("\n[μ
λ ₯ ν΄λ λ΄ νμΌ λͺ©λ‘]")
for entry in os.listdir(source):
full_path = os.path.join(source, entry)
modify_time = datetime.fromtimestamp(os.path.getmtime(full_path))
cutoff_time = datetime.now() - timedelta(days=10)
if os.path.isfile(full_path):
print(f" νμΌ - {entry} ({modify_time.strftime('%Y-%m-%d %H:%M:%S')})")
if modify_time <= cutoff_time:
try:
move_count = 0
shutil.move(full_path, destination)
move_count += 1
print(f" {entry}β μ΄λ μλ£")
except Exception as e:
print(f"μ΄λ μ€ν¨: {entry} ({e})")
continue
print(f"\n{move_count}κ° νμΌ μ΄λ μλ£.")