I want to delete a Folder named "testfolder" and all the subfolders and files in it. I want to give the "testfolder" path as a parameter when calling the python file. For example ...... (testfolder location) and there it should delete the "testfolder" when the folder exists
Asked
Active
Viewed 239 times
2
-
*If* this is the only requirement of the Python file, why not just use the shell? – S3DEV Jul 01 '20 at 10:59
-
Does this answer your question? [How to delete a file or folder?](https://stackoverflow.com/questions/6996603/how-to-delete-a-file-or-folder) – BDL Jul 01 '20 at 12:10
3 Answers
2
I guess this might be what you're looking for
import os
import shutil
pathLocation = # Whatever your path location is
if os.path.exists(pathLocation):
shutil.rmtree(pathLocation)
else:
print('the path doesn\'t exist')

Umutambyi Gad
- 4,082
- 3
- 18
- 39
-
Thank You for your answer @Gad. The path should not be in the file only the target folder name. The path where the targetfolder is should be set as a paramter when calling the python file, so i can use this programm for every path – noggy Jul 01 '20 at 10:51
-
@noggy You can upvote Gad's answer and mark it as accepted after a few minutes have passed. That's the best way to make folks coming here know your problem is solved. – user1717828 Jul 01 '20 at 10:52
1
You can use shutil.rmtree() for removing folders and argparse to get parameters.
import shutil
import argparse
import os
def remove_folder(folder_path='./testfolder/'):
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
print(f'{folder_path} and its subfolders are removed succesfully.')
else:
print(f'There is no such folder like {folder_path}')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Python Folder Remover')
parser.add_argument('--remove', '-r', metavar='path', required=True)
args = parser.parse_args()
if args.remove:
remove_folder(args.remove)
You can save above script as 'remove.py' and call it from command prompt like:
python remove.py --remove "testfolder"

Alihan ÖZ
- 173
- 1
- 7
-
is it possible to give the path where the testfolder is as a paramter when calling the script? So it should be python remove.py --remove ..\..\.. and there it should delete the folder testfolder? – noggy Jul 01 '20 at 11:24
-
If you meant to ask that: if you use python remove.py --remove "your_folder_path" or shortly python remove.py -r "your_folder_path" you can easily remove the folder and its subfolders. – Alihan ÖZ Jul 01 '20 at 12:10
-
is it also possible that i can give the folder path as a parameter but not the name of the folder so i can delete more folder like this. remove.py -r "your_folder_path and then there on this path it will delete al folder which are in the code e.g testfolder and testfolder2? How can i do that – noggy Jul 01 '20 at 19:42
-
you can loop your args.remove like that: if args.remove: for arg in args.remove.split(" "): remove_folder(args) and after that you can call python remove.py -r "testfolder testfolder2" – Alihan ÖZ Jul 02 '20 at 12:03
0
Better to use absolute path and import only the rmtree function from shutil import rmtree as this is a large package the above line will only import the required function.
from shutil import rmtree
rmtree('directory-absolute-path')

Umer Rana
- 148
- 6