Delete all file in folder python
How can I delete the contents of a local folder in Python? Show The current project is for Windows, but I would like to see *nix also.
martineau 115k25 gold badges160 silver badges284 bronze badges asked Oct 9, 2008 at 4:22
UnkwnTechUnkwnTech 85.2k65 gold badges183 silver badges227 bronze badges 3
Mark Amery 132k78 gold badges394 silver badges442 bronze badges answered Oct 9, 2008 at 4:27
Nick StinematesNick Stinemates 39.7k21 gold badges59 silver badges60 bronze badges 4 You can simply do this:
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,0886 gold badges47 silver badges60 bronze badges answered Apr 22, 2011 at 15:23
7 You can delete the folder itself, as well as all its contents, using
Mark Amery 132k78 gold badges394 silver badges442 bronze badges answered Oct 9, 2008 at 7:18
OliOli 14.7k8 gold badges30 silver badges36 bronze badges 11 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.
answered Jul 2, 2009 at 9:25
Iker JimenezIker Jimenez 6,9659 gold badges47 silver badges46 bronze badges 4 I'm surprised nobody has mentioned the awesome If you only want to remove files in a directory it can be a oneliner
To also recursively remove directories you can write something like this:
answered May 15, 2019 at 14:04
HuskyHusky 5,4422 gold badges43 silver badges40 bronze badges 3 Using The proposed solution using walk does not work as it uses The posted I suggest you use:
answered Jul 7, 2011 at 18:25
jgoedersjgoeders 1,85718 silver badges23 bronze badges 7 This:
Code:
As many other answers, this does not try to adjust permissions to enable removal of files/directories.
Mark Amery 132k78 gold badges394 silver badges442 bronze badges answered Sep 21, 2012 at 8:24
Jon ChuJon Chu 1,8572 gold badges19 silver badges19 bronze badges Using os.scandir and context manager protocol in Python 3.6+:
Earlier versions of Python:
answered Dec 2, 2012 at 5:51
Jacob WanJacob Wan 2,36321 silver badges19 bronze badges 4 Notes: in case someone down voted my answer, I have something to explain here.
Here's a long and ugly, but reliable and efficient solution. It resolves a few problems which are not addressed by the other answerers:
Here's the code (the only useful function is
answered Jul 19, 2014 at 20:21
RockalliteRockallite 15.9k6 gold badges53 silver badges48 bronze badges 1 As a oneliner:
A more robust solution accounting for files and directories as well would be (2.7):
answered Nov 24, 2013 at 11:22
fmonegagliafmonegaglia 2,6892 gold badges24 silver badges33 bronze badges 6 I used to solve the problem this way:
answered Jun 17, 2013 at 11:52
ProfHase85ProfHase85 11.3k7 gold badges46 silver badges61 bronze badges 12 To delete all the files inside the directory as well as its sub-directories, without removing the folders themselves, simply do this:
answered Feb 26, 2019 at 15:59
Kevin PatelKevin Patel 5168 silver badges10 bronze badges 1 To delete all files inside a folder a I use:
answered May 12, 2021 at 19:04
andrecandrec 1393 silver badges7 bronze badges 3 You might be better off using
answered Oct 9, 2008 at 5:52
mhawkemhawke 81.7k9 gold badges113 silver badges135 bronze badges 0 If you are using a *nix system, why not leverage the system command?
answered Jun 12, 2018 at 9:06
silverbulletttsilverbullettt 8161 gold badge10 silver badges13 bronze badges 5 I had to remove files from 3 separate folders inside a single parent directory:
This simple code did the trick for me: (I'm on Unix)
Hope this helps. answered Jan 11, 2020 at 11:34
NicoBarNicoBar 5251 gold badge7 silver badges14 bronze badges Yet Another Solution:
answered May 12, 2014 at 16:33
Robin WinslowRobin Winslow 10.4k8 gold badges58 silver badges89 bronze badges 1 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.
answered May 2, 2013 at 14:24
SawyerSawyer 5285 silver badges17 bronze badges 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.
answered Nov 4, 2019 at 18:59
Pretty intuitive way of doing it:
answered Jul 26, 2019 at 9:16
ManriqueManrique 1,9003 gold badges14 silver badges32 bronze badges 1 Use the method bellow to remove the contents of a directory, not the directory itself:
answered Feb 3, 2019 at 8:23
amrezzdamrezzd 1,56214 silver badges35 bronze badges 3 Answer for a limited, specific situation: assuming you want to delete the files while maintainig the subfolders tree, you could use a recursive algorithm:
Maybe slightly off-topic, but I think many would find it useful
Mark Amery 132k78 gold badges394 silver badges442 bronze badges answered Dec 27, 2016 at 10:55
fmonegagliafmonegaglia 2,6892 gold badges24 silver badges33 bronze badges 1 use this function
answered Sep 19 at 17:22
dazzafactdazzafact 2,0683 gold badges27 silver badges44 bronze badges I resolved the issue with
answered Mar 21, 2017 at 16:10
physlexicphyslexic 7861 gold badge8 silver badges20 bronze badges the easiest way to delete all files in a folder/remove all files
answered Jul 30, 2019 at 18:40
PyBossPyBoss 4677 silver badges18 bronze badges 1 This should do the trick just using the OS module to list and then remove!
Worked for me, any problems let me know! answered Jun 20, 2016 at 15:52
How do I delete all files in a directory in Python?To delete Directories. Use os.rmdir() or pathlib.Path.rmdir() to delete an empty directory.. use the shutil. rmtree() to recursively delete a directory and all files from it.. How do I delete multiple files in a directory in Python?To delete multiple files, just loop over your list of files and use the above os. rmdir() function. To delete a folder containing all files you want to remove have to import shutil package. Then you can remove the folder as follows.
How do you delete a file or folder in Python?Deleting a file or folder in Python. os. remove() removes a file.. os. unlink() removes a file. it is a Unix name of remove() method.. shutil. rmtree() deletes a directory and all its contents.. pathlib. Path. unlink() deletes a single file The pathlib module is available in Python 3.4 and above.. How do I delete everything in a folder?You can delete multiple files or folders by holding down the Ctrl key and clicking each file or folder before pressing Delete . You can hold down the Shift key while pressing the Delete key to prevent files from going to the Recycle Bin when deleted.
|