2017-05-07 13 views
0

python을 사용하여 폴더 자체를 삭제하지 않고 폴더 내의 모든 파일과 디렉토리를 삭제하는 가장 좋은 방법은 무엇입니까? 사용 :python 모든 파일을 (재귀 적으로) 폴더 자체없이 삭제

if os.path.exists("path/to/folder"): 
    shutil.rmtree("path/to/folder/*") 

윌하지 rmtree 이후 트릭() UNIX 스타일 (/ *)를 이해하지 못한다. rmtree 자체는 괜찮지 만 폴더가 삭제되었으므로 필요하지 않습니다.

+0

http://stackoverflow.com/questions/185936/delete-folder-contents-in-python – Aditya

답변

0

삭제 한 후에 폴더를 다시 만들 수 있습니다.

import os 
path = "/home/zach/Desktop" 
filename = "django" 
if os.path.exists(path): 
    os.system("cd %s" %path) 
    os.system("rm -rf %s" %filename) 
    print("deleted file") 
    os.system("mkdir %s" %filename) 
    print("created file") 
else: 
    print("error")