[英]Impossible to delete files in a loop with python
我想刪除文件夾中的文件,但出現錯誤。
我的密碼
for f in glob ('sub/*.sub'):
subprocess.call(["php", "AES.class.php" , f])
shutil.rmtree(f)
#deplacement des fichier
for d in glob ('*.ass'):
shutil.move(d, 'sync')
它給了我以下錯誤:
Traceback (most recent call last):
File "start.py", line 26, in <module>
shutil.rmtree(f)
File "/usr/lib64/python2.7/shutil.py", line 239, in rmtree
onerror(os.listdir, path, sys.exc_info())
File "/usr/lib64/python2.7/shutil.py", line 237, in rmtree
names = os.listdir(path)
OSError: [Errno 20] Not a directory: 'sub/Ep01.sub'
如何刪除文件夾中帶有.sub
擴展名的文件?
您需要os.remove
而不是shutil.rmtree
。 具體來說,前一種方法用於刪除文件 ,而后一種方法則用於刪除目錄 (及其所有內容)。
for f in glob ('sub/*.sub'):
subprocess.call(["php", "AES.class.php" , f])
os.remove(f)
#deplacement des fichier
for d in glob ('*.ass'):
shutil.move(d, 'sync')
這里有一個示例,使用Python刪除目錄中的所有文件
import os
filelist = [ f for f in os.listdir(".") if f.endswith(".bak") ]
for f in filelist:
subprocess.call(["php", "AES.class.php" , f])
os.remove(f)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.