파일 관리 - 이미지 처리 - pdf

(포토스케이프 일괄변환 위치 문제) -_- 해결

나도초딩 2022. 10. 21.
# 포토스케입 일괄변환 중, 하위폴더가 무수히 생기는 바람에... ㅠ
# 폴더100개마다 같은 이름의 하위폴더가 있는 구조

__모음__ 이라는 폴더를 만들어서, 지정한 하위 폴더를 집어넣는다. 이 때, 하위 폴더의 상위 폴더 이름으로 바꾼 후 옮긴다.
ex) 드래곤볼 | 리사이징

 

1) 하위 폴더인 "리사이징"의 이름을 "드래곤볼"로 바꾼다.
2) _모음_ 이라는 폴더를 만들어서, 부모폴더의 이름과 동일하게 바꾼 "드래곤볼" 폴더를 옮긴다.

 

os.path.isdir(), isfile() 디렉토리만 뽑기, 해당 파일/폴더 유무 확인 : 

if os.path.isdir():

os.rename() 파일,폴더 이동 :


파일,폴더 옮기기

os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

1. os.rename() : 바꿀 이름이 있으면 오류

2. os.replace() : 덮어 쓰기

3. shutil.move()

 
import os
from glob import glob
import shutil

subdir_name = input("옮길 하위 디렉토리 이름: ")
to_dir = os.path.join(os.getcwd(), "_모음_")

dir_only = [glob_list for glob_list in glob('*') if os.path.isdir(glob_list)]
cur_path = os.getcwd()

if not os.path.exists("_모음_"):
    os.makedirs("_모음_") # os.mkdir

for dir in dir_only:
    src = os.path.join(cur_path, dir,subdir_name) # dir 드래곤볼, subdir_name: 리사이징
    print(src)
    if os.path.isdir(src):
        dst = os.path.join(cur_path, "_모음_",dir)
        os.rename(src, dst)
        print(f"{dir} 완료", end=":")
    else:
        print(f"{dir}에는 {subdir_name} 폴더가 없습니다.")

댓글