Python Forum
delete all files and subdirectory from a main folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete all files and subdirectory from a main folder
#1
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
Reply
#2
You know the rules, show us your code and tell us what goes wrong. Then we will be glad to help you.
Larz60+ likes this post
Reply
#3
iberdeen, you're not being a good teammate! Let's go team!
Reply
#4
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/window...ommands/rd
Gribouillis likes this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Take a look at shutil.rmtree.
Larz60+ likes this post
Reply
#6
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
Reply
#7
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")
Reply
#8
(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.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 562 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 755 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,508 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,518 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 4,033 Oct-28-2022, 03:59 PM
Last Post: mg24
  python run all py files from main py file mg24 6 1,350 Oct-12-2022, 04:41 AM
Last Post: mg24
  Merge all json files in folder after filtering deneme2 10 2,373 Sep-18-2022, 10:32 AM
Last Post: deneme2
  delete all files which contains word sql_Table1 mg24 2 873 Sep-15-2022, 10:05 PM
Last Post: mg24
  Delete empty text files [SOLVED] AlphaInc 5 1,579 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Delete files from amazon s3 bucket python_student 0 3,932 Jan-14-2022, 04:51 PM
Last Post: python_student

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020