0

I have a list of folders with files in the following path: C:\AutoTest\engine\Scv\Projects

In the Projects folder there are folders with files. E.g. Projects\selenium_project1\files Projects\Python_project1\files etc.

I would like to delete all the folders from the Projects folders and files. e.g. I would like to delete selenium_project1\files and Python_project1\files etc.

I have found the following code to delete files. I am not sure if this will do it correctly. Will the code below delete the folders and files from the Projects directory?

import os, shutil
folder = '/path/to/folder'
for the_file in os.listdir(folder):
    file_path = os.path.join(folder, the_file)
    try:
        if os.path.isfile(file_path):
            os.unlink(file_path)
    except Exception as e:
        print(e)

Thanks, Riaz

1
  • 2
    Why not replace os.unlink(file_path) with print(file_path) and see what would be deleted? And no, it won't delete the folders, only the files. Commented Apr 18, 2016 at 11:42

1 Answer 1

1

I have managed to do it in the following way. My code function is below:

@staticmethod
def deleteProjects():
    #projects = os.listdir(os.path.join(ENGINE_DIR, r"scv\Projects"))
    ENGINE_DIR = r"C:\AutoTest\engine"
    projects = os.listdir(os.path.join(ENGINE_DIR, r"scv\Projects"))
    for project in projects:
        logging.error("Project %s not deleted" % project)
        path = os.path.join(os.path.join(ENGINE_DIR, r"scv\Projects"), project)
        print "*** path ***"
        print path
        shutil.rmtree(path)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.