0

I want delete files on a folder and I have an error.

My code

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')

It gives me the following error:

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'

How to delete the files with extension .sub in folder?

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
kibaya
  • 93
  • 1
  • 4
  • 9

2 Answers2

3

You want os.remove rather than shutil.rmtree. Specifically, the former method is for removing a file whereas the latter is designed to remove a directory (along with all of it's contents).

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')
mgilson
  • 300,191
  • 65
  • 633
  • 696
2

You have an exemple here Deleting all files in a directory with 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)
Community
  • 1
  • 1
Kantium
  • 512
  • 4
  • 12