簡體   English   中英

如何使用 python 真正刪除文件夾中的所有文件和 Windows 上的文件夾?

[英]How to really delete all files in a folder and the folder on Windows using python?

這不會刪除文件夾

shutil.rmtree('C:\\Users\\0\\Downloads\\preoutput')

這會導致此錯誤 Message=[WinError 5] Access is denied: 'C:\Users\0\Downloads\testdelete'

import os
import glob

files = glob.glob('C:\\Users\\0\\Downloads\\preoutput')
for f in files:
    #os.chmod(f, 0o777)
    os.remove(f)

嘗試使用 shutil.rmtree() 來實現這個程序

import shutil
import os 

#location 
location = "C:\\Users\\0\\Downloads\\preoutput"
#directory 
dir = "preoutput"
#path 
path = os.path.join(location, dir) 
#removing directory 
shutil.rmtree(path)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM