Python Forum
delete all files and subdirectory from a main folder - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: delete all files and subdirectory from a main folder (/thread-38545.html)



delete all files and subdirectory from a main folder - mg24 - Oct-27-2022

Hi Team,

I want to delete all files and subfolder from a main directory.
D:\\Data\abc.txt,abc.csv,xyz.pdf etc.
Delete all content from Data folder.



Thanks
mg


RE: delete all files and subdirectory from a main folder - ibreeden - Oct-27-2022

You know the rules, show us your code and tell us what goes wrong. Then we will be glad to help you.


RE: delete all files and subdirectory from a main folder - deanhystad - Oct-27-2022

iberdeen, you're not being a good teammate! Let's go team!


RE: delete all files and subdirectory from a main folder - wavic - Oct-27-2022

If that is a general question, I can't see how it is related to Python.
I see the Windows file system here so we should look at del command.

Anyway, not so familiar with cmd or PowerShell so I did a brief research.
There is that rd command that can do it like a charm: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rd


RE: delete all files and subdirectory from a main folder - bowlofred - Oct-27-2022

Take a look at shutil.rmtree.


RE: delete all files and subdirectory from a main folder - mg24 - Oct-28-2022

Hi Team,

I am using below code, but file is not getting deleted.
print(file) is printing below.

def Delete_Folder(site,tbl,Constant_folder):
    import os
    path = f"{Constant_folder}\{site}\{tbl}"

    for file_name in os.listdir(path):

        # construct full file path
        file = path + file_name
        print(file)

        if os.path.isfile(file):
            print('Deleting file:', file)
            os.remove(file)

if __name__ == "__main__":
    Delete_Folder("AU","tbl5","D:\Python\sample_folder")
output

D:\Python\venv\Scripts\python.exe D:\Python\test2.py
D:\Python\sample_folder\AU\tbl5abc - Copy (2).txt
D:\Python\sample_folder\AU\tbl5abc - Copy (3).txt
D:\Python\sample_folder\AU\tbl5abc - Copy (4).txt
D:\Python\sample_folder\AU\tbl5abc - Copy (5).txt
D:\Python\sample_folder\AU\tbl5abc - Copy (6).txt
D:\Python\sample_folder\AU\tbl5abc - Copy (7).txt
D:\Python\sample_folder\AU\tbl5abc - Copy.txt
D:\Python\sample_folder\AU\tbl5abc.txt


RE: delete all files and subdirectory from a main folder - mg24 - Oct-28-2022

Hi Team,

this code delete all files, got solution.


def Delete_Folder(site,tbl,Constant_folder):
    import os
    import glob
    path = f"{Constant_folder}\{site}\{tbl}"

    os.chdir(path)
    files = glob.glob('*.*')
    for filename in files:
        os.unlink(filename)


if __name__ == "__main__":
    Delete_Folder("us","tbl4","D:\Python\sample_folder")



RE: delete all files and subdirectory from a main folder - ibreeden - Oct-28-2022

(Oct-28-2022, 03:41 AM)mg24 Wrote: this code delete all files, got solution.
Thank you for letting us know you found the solution. And also you did right showing the solution, it may help other people with the same problem.

But I have some things you should think about.
  1. Your function "Delete_Folder" does not delete the folder.
  2. There may be a problem to delete the folder, because you did a chdir() to that folder. If I remember well, you cannot delete your current folder.
  3. Looking at your output, I do not see the sub-folder "tbl4".
    Output:
    D:\Python\venv\Scripts\python.exe D:\Python\test2.py D:\Python\sample_folder\AU\tbl5abc - Copy (2).txt D:\Python\sample_folder\AU\tbl5abc - Copy (3).txt
  4. I thought you meant with the parameter "tbl" to delete files, whose names start with "tbl".
  5. If the previous assumption is right, you cannot delete the folder if other files are remaining in the folder. (Or if you can, then the other files are also deleted so it is not necessary to delete files first.)