본문 바로가기

Computer

[Python] 하위 폴더에서 상위 폴더로 파일 이동, 하위 폴더 삭제

 

- 0. 만들었다.

닌텐도 스위치 앨범 확인 시 년도/월별/일별로 나뉘어서 사진이 저장되어 있어 확인이 굉장히 불편했음.

월별 폴더에 모아놓고 일별 폴더는 지우도록 코드를 구현했다.

import os
import shutil
allpath = 'C:\\BIN\닌텐도'
years = os.listdir(allpath)#years
for i in years:
    ypath = allpath + '\\' + i #i year
    year = os.listdir(ypath)
    for j in year:
        mpath = ypath+'\\'+j  #j month
        #os.chdir(mpath)
        month = os.listdir(mpath)
        for k in month:
            dpath = mpath+'\\' + k #k day
            if os.path.isdir(dpath):
                os.chdir(dpath)
                day = os.listdir(dpath)
                for l in day:
                    shutil.move(dpath+'\\'+l, mpath+'\\'+l)
                os.chdir(mpath)
                os.rmdir(dpath)

 닌텐도 폴더 위치만 변경해 주면 됨.

잘 옮겨지는것을 확인했다.

 

- 1. 참고 사이트 

파일 이동 : https://ourcstory.tistory.com/96