import os
root_path = r"C:\cjs_work\film\pat"
root_path1 = r"C:\cjs_work\film\silk&mask"
# 함수 정의: 디렉토리를 재귀적으로 탐색하면서 파일 삭제
def delete_files(dir_path):
for root, dirs, files in os.walk(dir_path):
for file in files:
if "comp" not in file and "sold" not in file:
os.remove(os.path.join(root, file))
def delete_files1(dir_path):
for root, dirs, files in os.walk(dir_path):
for file in files:
if "cslk" not in file and "csmk" not in file and "sslk" not in file and "ssmk" not in file and "jepan" not in file and "plug" not in file:
os.remove(os.path.join(root, file))
# 루트 디렉토리에서 함수 호출
delete_files(root_path)
delete_files1(root_path1)
이 코드는 Python으로 작성된 스크립트로, delete_files와 delete_files1 두 함수를 정의하고,
이를 이용하여 두 개의 다른 디렉토리 내에서 특정 파일을 재귀적으로 삭제합니다.
첫 번째 디렉토리는 root_path 변수로 지정되며, "C:\cjs_work\film\pat"으로 설정됩니다.
두 번째 디렉토리는 root_path1 변수로 지정되며, "C:\cjs_work\film\silk&mask"로 설정됩니다.
delete_files 함수는 디렉토리 경로(dir_path)를 입력으로 받아 해당 디렉토리를 재귀적으로 탐색하면서, 파일 이름에 "comp" 또는 "sold" 문자열이 포함되어 있지 않은 모든 파일을 삭제합니다. 파일 삭제에는 os.remove 함수가 사용됩니다.
delete_files1 함수는 비슷한 방식으로 작동하지만, 두 번째 디렉토리(root_path1)에서 파일을 삭제합니다. 이 함수는 파일 이름에 "cslk", "csmk", "sslk", "ssmk", "jepan", "plug" 문자열을 포함하지 않은 모든 파일을 삭제합니다.
마지막으로, 이 스크립트는 각각의 디렉토리 경로를 입력으로 하여 두 함수를 호출하여
각 디렉토리에서 지정된 파일을 삭제합니다.
========================================================
각 두개의 폴더에 있는 특정 파일만 남기고 다 삭제하는 프로그램 예제
매번 여러파일중 일부 이름만 있는 파일만 남기고 싶을때 활용하면 될거같다.
'파이썬' 카테고리의 다른 글
| print 관련 (0) | 2023.11.27 |
|---|---|
| 파이썬 Arrow 예제 (0) | 2023.05.18 |
| 윈도우 창을 띄우는 예제 (0) | 2023.05.09 |
| 들여쓰기, 주석, 세미콜론 (0) | 2023.04.26 |
| 파이썬이란?? (0) | 2023.04.23 |