788

How can I delete the contents of a local folder in Python?

The current project is for Windows, but I would like to see *nix also.

martineau
  • 119,623
  • 25
  • 170
  • 301
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
  • 20
    for *nix to be honest i would just use `os.system('rm -rf folder')` – Tilak Madichetti Nov 23 '19 at 14:53
  • 21
    @TilakMaddy That's a terrible way, especially if you take the folder name as user input. Unfortunately we can't downvote comments. – alexia Apr 03 '22 at 13:47
  • 5
    The comment by @TilakMaddy is a painless solution, but it fails to address 'the contents' part. `os.system('rm -rf folder/*')` is the way to go, assuming there are no `.*` contents... – tishma May 24 '22 at 13:07
  • We should add a new argument in `shutil.rmtree` (ex ignore_main_folder/preserve_main_folder). This is often useful when you don't have the user right to create the main folder. – Kiruahxh Nov 10 '22 at 13:17

27 Answers27

678
import os, shutil
folder = '/path/to/folder'
for filename in os.listdir(folder):
    file_path = os.path.join(folder, filename)
    try:
        if os.path.isfile(file_path) or os.path.islink(file_path):
            os.unlink(file_path)
        elif os.path.isdir(file_path):
            shutil.rmtree(file_path)
    except Exception as e:
        print('Failed to delete %s. Reason: %s' % (file_path, e))
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
  • 12
    If you're working with a very large directory, and particularly a network directory on windows, and you can control the environment in which this program runs, it might be worth using Py3.5's "os.scandir(folder)" function instead of listdir. The syntax is quite different after that, but pretty simple to implement; happy to post it if others want. – Michael Scott Asato Cuthbert Apr 02 '16 at 14:34
  • 5
    I'm getting a pylint warning with `except Exception as e:` that reads `W0703: Catching too general exception Exception`. Is there a more specific Exception to catch or should I ignore it? – John Hany Oct 23 '17 at 05:04
  • 17
    @JohnHany, I believe you want to catch OSError. – MikeB Apr 07 '18 at 20:13
  • I'm getting OSError: [Errno 26] Text file busy My file structure: Media(like folder in your code) contains->2 files and 2 directories where both directories contains each one file – Vanjith Jun 19 '20 at 20:34
  • Why do you use os.unlink instead of os.remove? – openCivilisation Apr 17 '23 at 04:55
479

You can simply do this:

import os
import glob

files = glob.glob('/YOUR/PATH/*')
for f in files:
    os.remove(f)

You can of course use an other filter in you path, for example : /YOU/PATH/*.txt for removing all text files in a directory.

John Smith
  • 7,243
  • 6
  • 49
  • 61
Blueicefield
  • 5,269
  • 1
  • 16
  • 8
354

You can delete the folder itself, as well as all its contents, using shutil.rmtree:

import shutil
shutil.rmtree('/path/to/folder')
shutil.rmtree(path, ignore_errors=False, onerror=None)


Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Oli
  • 15,345
  • 8
  • 30
  • 36
  • 424
    This will not only delete the contents but the folder itself as well. I don't think it is what the question asks. – Iker Jimenez Jul 01 '09 at 17:14
  • 4
    I think it's a good answer. Why don't you just delete the contents and the folder, and then remake the folder? – cssndrx Jul 06 '11 at 20:30
  • 72
    Because the new directory and the old one will not be the same. So if a program is sitting in the directory, waiting for things, it will have the rug pulled out from under it. – Mike Cooper Nov 26 '11 at 19:09
  • 1
    Note also that if there is one file in somewhere down the folder tree that can't be deleted, nothing gets deleted. – James Feb 18 '13 at 13:57
  • 52
    Just recreate the directory after `rmtree`. Like `os.makedirs(dir)` – Iulius Curt Jun 14 '13 at 07:52
  • 19
    @IuliusCurt no, I have a directory mounted in ram I need to empty, and unfortunately I can't just delete then re-create it: `OSError: [Errno 16] Device or resource busy` – Arnaud P Dec 13 '18 at 14:56
  • 14
    You have to note that recreating the folder is not necessarily the same, e.g if you mount a folder into a docker and remove the folder, recreating a folder inside the docker will not result in recreating the mounted folder. – ethanjyx Sep 13 '19 at 15:58
  • 8
    Also consider permissions, file mods in unix, ownerships etc. Therefore this example is dangerous. – Saren Tasciyan Mar 31 '20 at 11:19
  • 2
    plus you will reset any metadata (creation time, last access time etc) – Daniel Braun May 06 '20 at 18:16
  • 1
    Even if you don't care about metadata etc., on Windows there can randomly be some timing issue where if you do shutil.rmtree() immediately followed by os.makedirs(..., exist_ok=True), makedirs will think the folder still exists, so it won't create a folder. However, the folder will be removed after the makedirs call finishes. This was mentioned in https://stackoverflow.com/a/6615332/848627 regarding network drives, but I've seen it happen on a normal drive as well. – drojf Aug 29 '20 at 01:48
  • using this because `os.remove()` does not work, running Mac on Big Sur – cryanbhu Dec 10 '20 at 10:04
120

Expanding on mhawke's answer this is what I've implemented. It removes all the content of a folder but not the folder itself. Tested on Linux with files, folders and symbolic links, should work on Windows as well.

import os
import shutil

for root, dirs, files in os.walk('/path/to/folder'):
    for f in files:
        os.unlink(os.path.join(root, f))
    for d in dirs:
        shutil.rmtree(os.path.join(root, d))
Iker Jimenez
  • 7,105
  • 9
  • 49
  • 46
  • 1
    Why 'walk' and not just list folder content? – Don Jan 18 '12 at 14:40
  • 3
    This is the correct answer if you want to delete directories as well. `walk` is used to split dirs vs. files, which must be handled differently. You could also use `os.listdir`, but you'd have to check if each entry is a dir or file manually. – dkamins Mar 16 '12 at 02:29
  • 7
    This is close, but both os.walk and shutil.rmtree are recursive. os.walk is unnecessary since you only need the files and directories at the top level inside the directory to be cleaned. Just use an if statement on elements in os.listdir to see if each is a file or directory. Then use remove/unlink and rmtree respectively. – Matthew Alpert Jan 08 '14 at 23:57
  • 6
    @MatthewAlpert Note, however, that `os.walk` won't recurse here, because it returns a generator the only recursively looks into subdirectories when you try to advance it, and by the time you've done your first iteration of this loop, there are no subdirectories left to look at. In essence, `os.walk` is just being used here as an alternative way of distinguishing top-level folders from top-level files; the recursion isn't being used and we pay no performance cost for it. It's eccentric, though, and I agree that the approach you suggest is better simply because it's more explicit and readable. – Mark Amery Nov 23 '19 at 14:31
77

I'm surprised nobody has mentioned the awesome pathlib to do this job.

If you only want to remove files in a directory it can be a oneliner

from pathlib import Path

[f.unlink() for f in Path("/path/to/folder").glob("*") if f.is_file()] 

To also recursively remove directories you can write something like this:

from pathlib import Path
from shutil import rmtree

for path in Path("/path/to/folder").glob("**/*"):
    if path.is_file():
        path.unlink()
    elif path.is_dir():
        rmtree(path)
Leland Hepworth
  • 876
  • 9
  • 16
Husky
  • 5,757
  • 2
  • 46
  • 41
  • 9
    `.iterdir()` instead of `.glob(...)` should also work. – S. Kirby Nov 14 '19 at 01:30
  • 5
    Not best practice to use list comprehension this way. List comprehension is for building lists. The second method is good. – DonkeyKong Jun 30 '22 at 17:38
  • 1
    To make it compatible with Win and Unix, consider starting from your current directory and using the magic of PathLib to navigate. This is just a hint, not an answer. – NFSpeedy Dec 20 '22 at 13:03
51

Using rmtree and recreating the folder could work, but I have run into errors when deleting and immediately recreating folders on network drives.

The proposed solution using walk does not work as it uses rmtree to remove folders and then may attempt to use os.unlink on the files that were previously in those folders. This causes an error.

The posted glob solution will also attempt to delete non-empty folders, causing errors.

I suggest you use:

folder_path = '/path/to/folder'
for file_object in os.listdir(folder_path):
    file_object_path = os.path.join(folder_path, file_object)
    if os.path.isfile(file_object_path) or os.path.islink(file_object_path):
        os.unlink(file_object_path)
    else:
        shutil.rmtree(file_object_path)
jgoeders
  • 1,886
  • 19
  • 25
  • *"The proposed solution using walk does not work as it uses `rmtree` to remove folders and then may attempt to use `os.unlink` on the files that were previously in those folders"* - this isn't what I witness using a local drive on my Mac. The generator returned from `os.walk` simply doesn't yield a second element if you've deleted all the folders on the first iteration. I can believe that there's some file system or environment for which this is true (e.g. a network drive), but it's definitely not a universal truth. – Mark Amery Nov 23 '19 at 14:37
  • 1
    Also: @kevinf is correct to point out the need for an `islink` check here to handle symlinks to directories correctly. I've added such a check to the accepted answer. – Mark Amery Nov 23 '19 at 14:46
28

This:

  • removes all symbolic links
    • dead links
    • links to directories
    • links to files
  • removes subdirectories
  • does not remove the parent directory

Code:

for filename in os.listdir(dirpath):
    filepath = os.path.join(dirpath, filename)
    try:
        shutil.rmtree(filepath)
    except OSError:
        os.remove(filepath)

As many other answers, this does not try to adjust permissions to enable removal of files/directories.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Jon Chu
  • 1,877
  • 2
  • 20
  • 19
28

Using os.scandir and context manager protocol in Python 3.6+:

import os
import shutil

with os.scandir(target_dir) as entries:
    for entry in entries:
        if entry.is_dir() and not entry.is_symlink():
            shutil.rmtree(entry.path)
        else:
            os.remove(entry.path)

Earlier versions of Python:

import os
import shutil

# Gather directory contents
contents = [os.path.join(target_dir, i) for i in os.listdir(target_dir)]

# Iterate and remove each item in the appropriate manner
[shutil.rmtree(i) if os.path.isdir(i) and not os.path.islink(i) else os.remove(i) for i in contents]
Jacob Wan
  • 2,521
  • 25
  • 19
18

Notes: in case someone down voted my answer, I have something to explain here.

  1. Everyone likes short 'n' simple answers. However, sometimes the reality is not so simple.
  2. Back to my answer. I know shutil.rmtree() could be used to delete a directory tree. I've used it many times in my own projects. But you must realize that the directory itself will also be deleted by shutil.rmtree(). While this might be acceptable for some, it's not a valid answer for deleting the contents of a folder (without side effects).
  3. I'll show you an example of the side effects. Suppose that you have a directory with customized owner and mode bits, where there are a lot of contents. Then you delete it with shutil.rmtree() and rebuild it with os.mkdir(). And you'll get an empty directory with default (inherited) owner and mode bits instead. While you might have the privilege to delete the contents and even the directory, you might not be able to set back the original owner and mode bits on the directory (e.g. you're not a superuser).
  4. Finally, be patient and read the code. It's long and ugly (in sight), but proven to be reliable and efficient (in use).

Here's a long and ugly, but reliable and efficient solution.

It resolves a few problems which are not addressed by the other answerers:

  • It correctly handles symbolic links, including not calling shutil.rmtree() on a symbolic link (which will pass the os.path.isdir() test if it links to a directory; even the result of os.walk() contains symbolic linked directories as well).
  • It handles read-only files nicely.

Here's the code (the only useful function is clear_dir()):

import os
import stat
import shutil


# http://stackoverflow.com/questions/1889597/deleting-directory-in-python
def _remove_readonly(fn, path_, excinfo):
    # Handle read-only files and directories
    if fn is os.rmdir:
        os.chmod(path_, stat.S_IWRITE)
        os.rmdir(path_)
    elif fn is os.remove:
        os.lchmod(path_, stat.S_IWRITE)
        os.remove(path_)


def force_remove_file_or_symlink(path_):
    try:
        os.remove(path_)
    except OSError:
        os.lchmod(path_, stat.S_IWRITE)
        os.remove(path_)


# Code from shutil.rmtree()
def is_regular_dir(path_):
    try:
        mode = os.lstat(path_).st_mode
    except os.error:
        mode = 0
    return stat.S_ISDIR(mode)


def clear_dir(path_):
    if is_regular_dir(path_):
        # Given path is a directory, clear its content
        for name in os.listdir(path_):
            fullpath = os.path.join(path_, name)
            if is_regular_dir(fullpath):
                shutil.rmtree(fullpath, onerror=_remove_readonly)
            else:
                force_remove_file_or_symlink(fullpath)
    else:
        # Given path is a file or a symlink.
        # Raise an exception here to avoid accidentally clearing the content
        # of a symbolic linked directory.
        raise OSError("Cannot call clear_dir() on a symbolic link")
Rockallite
  • 16,437
  • 7
  • 54
  • 48
  • 1
    I don't understand in what context changing the file mode makes sense. On my Mac, `os.remove`, *unlike* the `rm` utility, is happy to delete read-only files as long as you own them. Meanwhile, if it's a file you *don't* own that you only have readonly access to, then you can't delete it *or* change its permissions. I don't know any situation on any system where you'd be unable to delete a readonly file with `os.remove` yet be able to change its permissions. Furthermore, you use `lchmod`, which doesn't exist on my Mac, nor on Windows according to its docs. What platform is this code meant for?! – Mark Amery Nov 23 '19 at 15:03
17

I used to solve the problem this way:

import shutil
import os

shutil.rmtree(dirpath)
os.mkdir(dirpath)
Nouman
  • 6,947
  • 7
  • 32
  • 60
ProfHase85
  • 11,763
  • 7
  • 48
  • 66
  • 16
    This has radically different semantics from what the question asks and shouldn't be considered as a valid answer. – fatuhoku Sep 26 '13 at 15:22
  • 1
    Don't understand your comment: > " How can I delete the contents of a local folder in Python. The current project is for Windows but I would like to see *nix also. " is the question. The answer solves the task. – ProfHase85 Sep 26 '13 at 15:28
  • 3
    With respect I think "Delete the contents of the local folder" doesn't involve removing the folder itself. Same problem as [this answer](http://stackoverflow.com/a/186236/590767), except that one got lots of upvotes! – fatuhoku Sep 26 '13 at 19:43
  • 5
    It's like answering the question "How do I have a function return the number 1 in Python?" with def return_a_one(): launch_some_nukes() return 1 – fatuhoku Sep 26 '13 at 19:44
  • 3
    Of course the semantics is different: But you might as well consider it as another way to look at the problem. This solution is perfectly valid as it solves the problem. There is a difference to your 'launch_some_nukes' example: 1. The solution is shorter and easier than the accepted one and in oppose to the answer you quoted it is valid. 2. the 'launch_some_nukes' equivalent in this case is deleting and recreating a folder. The difference between the old and the new folder is only the inode number(probably irrelevant for the OP) – ProfHase85 Oct 31 '13 at 00:22
  • I would rather say it is more like the typical physical question: find out the hight of a skyscraper with the help of a barometer: the expected solution is to measure the atmospheric pressure downstairs and on top and calculate the hight difference using barometric formula The other solution would be to go to the janitor and to give him the barometer in return for telling you the hight of the skyscraper. – ProfHase85 Oct 31 '13 at 00:28
  • 2
    Neither involves demolishing the skyscraper, then rebuilding one of a height of your choosing. – fatuhoku Oct 31 '13 at 13:19
  • 2
    It is rather demolishing the skyscraper and rebuilding one exactly the same size ;) – ProfHase85 Oct 31 '13 at 14:25
  • 2
    I actually used this solution - and it works. On the plus side - it is simple, on the downside - there are some edge cases. Deleting the Directory will fail (on Windows at least) if have a shell open in that directory - deleting the files will NOT cause this. Also: when using relative names for the directory such as "temp", the 'rmtree' will work, but the 'os.mkdir' will fail (unless you use "./temp"). – monojohnny Oct 07 '15 at 15:50
  • Brilliant Idea, but theres a problem, it may not work sometimes: https://stackoverflow.com/questions/16373747/permission-denied-doing-os-mkdird-after-running-shutil-rmtreed-in-python – Nouman Oct 13 '19 at 15:32
  • 1
    how do you keep user privelages of the folder? – obayhan Oct 15 '20 at 12:05
  • Well depending on the situation this works pretty well @fatuhoku, example in my case I am trying to do as what is being questioned but depending on my program I would think if deleting and re-creating the directory would cause some issues `(that's on me)` but it does not.. so this answer is still Okay! Depends who reads it on the internet and how they would think this answer would affect their program. – Ice Bear Dec 17 '20 at 04:45
17

As a oneliner:

import os

# Python 2.7
map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) )

# Python 3+
list( map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) ) )

A more robust solution accounting for files and directories as well would be (2.7):

def rm(f):
    if os.path.isdir(f): return os.rmdir(f)
    if os.path.isfile(f): return os.unlink(f)
    raise TypeError, 'must be either file or directory'

map( rm, (os.path.join( mydir,f) for f in os.listdir(mydir)) )
fmonegaglia
  • 2,749
  • 2
  • 24
  • 34
  • 1
    for large operations using the generator may be fractionally more efficient ```map( os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir)) )``` – user25064 Sep 18 '14 at 15:36
  • actually trying to use this, realized the map object must be iterated over so a call to list (or something that will iterate) is required like ```list(map(os.unlink, (os.path.join( mydir,f) for f in os.listdir(mydir))))``` – user25064 Sep 19 '14 at 14:19
  • First one included in the answer, second one makes no sense to me. Why should you iterate over a function mapped to an iterable? Map does that. – fmonegaglia Oct 22 '15 at 09:57
  • 2
    In Python3, you have to wrap `map` in `list` to actually iterate. See [http://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x](http://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-x) – paulwasit Oct 07 '16 at 08:30
13

To delete all files inside a folder a I use:

import os
for i in os.listdir():
    os.remove(i)
andrec
  • 168
  • 3
  • 8
  • 10
    Just to stress that this will only work if the given directory only contains files. If the directory contains another directory, an `IsADirectoryError` exception will be raised. – piit79 May 12 '21 at 20:09
  • 3
    This answer is not technically correct. "i" will be the name of the item only and will not include the directory path. Suggested correction: for i in os.listdir(dir_path): os.remove(fr'{dir_path}\{i}'). – DonkeyKong Jun 30 '22 at 17:51
12

To delete all the files inside the directory as well as its sub-directories, without removing the folders themselves, simply do this:

import os
mypath = "my_folder" #Enter your path here
for root, dirs, files in os.walk(mypath, topdown=False):
    for file in files:
        os.remove(os.path.join(root, file))

    # Add this block to remove folders
    for dir in dirs:
        os.rmdir(os.path.join(root, dir))

# Add this line to remove the root folder at the end
os.rmdir(mypath)
Shravya Boggarapu
  • 572
  • 1
  • 7
  • 23
Kevin Patel
  • 605
  • 10
  • 11
  • This answer is brilliant but could use some more details. Add topdown = false ins os.walk and after removing files, remove directory too – Shravya Boggarapu Sep 13 '22 at 05:03
9

If you are using a *nix system, why not leverage the system command?

import os
path = 'folder/to/clean'
os.system('rm -rf %s/*' % path)
silverbullettt
  • 846
  • 1
  • 10
  • 13
8

You might be better off using os.walk() for this.

os.listdir() doesn't distinguish files from directories and you will quickly get into trouble trying to unlink these. There is a good example of using os.walk() to recursively remove a directory here, and hints on how to adapt it to your circumstances.

mhawke
  • 84,695
  • 9
  • 117
  • 138
6

I konw it's an old thread but I have found something interesting from the official site of python. Just for sharing another idea for removing of all contents in a directory. Because I have some problems of authorization when using shutil.rmtree() and I don't want to remove the directory and recreate it. The address original is http://docs.python.org/2/library/os.html#os.walk. Hope that could help someone.

def emptydir(top):
    if(top == '/' or top == "\\"): return
    else:
        for root, dirs, files in os.walk(top, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))
Sawyer
  • 568
  • 5
  • 17
6

I had to remove files from 3 separate folders inside a single parent directory:

directory
   folderA
      file1
   folderB
      file2
   folderC
      file3

This simple code did the trick for me: (I'm on Unix)

import os
import glob

folders = glob.glob('./path/to/parentdir/*')
for fo in folders:
  file = glob.glob(f'{fo}/*')
  for f in file:
    os.remove(f)

Hope this helps.

NicoBar
  • 555
  • 1
  • 7
  • 16
5

Yet Another Solution:

import sh
sh.rm(sh.glob('/path/to/folder/*'))
Robin Winslow
  • 10,908
  • 8
  • 62
  • 91
  • 5
    Note that `sh` isn't part of the standard library and needs installing from PyPI before you can use it. Also, since this actually invokes `rm` in a subprocess, it won't work on Windows where `rm` doesn't exist. It will also raise an exception if the folder contains any subdirectories. – Mark Amery Nov 23 '19 at 15:51
4

Well, I think this code is working. It will not delete the folder and you can use this code to delete files having the particular extension.

import os
import glob

files = glob.glob(r'path/*')
for items in files:
    os.remove(items)
Kush Modi
  • 51
  • 3
2

Pretty intuitive way of doing it:

import shutil, os


def remove_folder_contents(path):
    shutil.rmtree(path)
    os.makedirs(path)


remove_folder_contents('/path/to/folder')
Manrique
  • 2,083
  • 3
  • 15
  • 38
2

use this function

import os
import glob

def truncate(path):
    files = glob.glob(path+'/*.*')
    for f in files:
        os.remove(f)

truncate('/my/path')
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
dazzafact
  • 2,570
  • 3
  • 30
  • 49
2

Other methods I tried with os and glob package, I had permission issue but with this I had no permission issue plus one less package usage. Probably fail if sub directory exist.

import os

def remove_files_in_folder(folderPath):
        # loop through all the contents of folder
        for filename in os.listdir(folderPath):
            # remove the file
            os.remove(f"{folderPath}/{filename}")

remove_files_in_folder('./src/inputFiles/tmp')

Folder structure

root
  |
  +-- main.py
  |
  +-- src
       |
       +-- inputFiles
            |
            +-- tmp
                 |
                 +-- file1.txt
                 +-- img1.png
zorro
  • 101
  • 1
  • 10
1

Answer for a limited, specific situation: assuming you want to delete the files while maintainig the subfolders tree, you could use a recursive algorithm:

import os

def recursively_remove_files(f):
    if os.path.isfile(f):
        os.unlink(f)
    elif os.path.isdir(f):
        for fi in os.listdir(f):
            recursively_remove_files(os.path.join(f, fi))

recursively_remove_files(my_directory)

Maybe slightly off-topic, but I think many would find it useful

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
fmonegaglia
  • 2,749
  • 2
  • 24
  • 34
  • 1
    Using `os.walk` in the way shown at https://stackoverflow.com/a/54889532/1709587 is perhaps a nicer way of deleting all files while leaving the directory structure intact. – Mark Amery Nov 23 '19 at 15:42
1

Use the method bellow to remove the contents of a directory, not the directory itself:

import os
import shutil

def remove_contents(path):
    for c in os.listdir(path):
        full_path = os.path.join(path, c)
        if os.path.isfile(full_path):
            os.remove(full_path)
        else:
            shutil.rmtree(full_path)
amrezzd
  • 1,787
  • 15
  • 38
-1

I resolved the issue with rmtree makedirs by adding time.sleep() between:

if os.path.isdir(folder_location):
    shutil.rmtree(folder_location)

time.sleep(.5)

os.makedirs(folder_location, 0o777)
Carlos Mermingas
  • 3,822
  • 2
  • 21
  • 40
physlexic
  • 826
  • 2
  • 9
  • 21
-2

the easiest way to delete all files in a folder/remove all files

import os
files = os.listdir(yourFilePath)
for f in files:
    os.remove(yourFilePath + f)
PyBoss
  • 599
  • 1
  • 7
  • 20
-5

This should do the trick just using the OS module to list and then remove!

import os
DIR = os.list('Folder')
for i in range(len(DIR)):
    os.remove('Folder'+chr(92)+i)

Worked for me, any problems let me know!

B. Filer
  • 5
  • 2
  • I very much doubt this answer worked for you, unless your `Folder` had only a sequence of files named `0`, `1`, `2`, `3`... in it, and you had a special python interpreter with a custom `os.list` function in it :) – sox supports the mods Mar 27 '23 at 14:09