Python Tutorial: File Deletion and Folder Deletion / directory deletion
Skills:
AI Workflow Automation60%
Key Takeaways
Deleting files and directories using Python's os module
Full Transcript
hello and welcome to another Python tutorial in this tutorial we're going to be talking about how to manipulate actual files so how do we delete rename and move files and even directories so with that let's get into it let's make a fake directory here and we'll just call this um whole folder and within whole folder we're going to make just some random stuff another folder and then we're going to have a um Let's do an image and then we'll just make an extra text document here okay so now we have stuff within this whole folder so the next thing we want to do is delete stuff like let's delete something from that folder so the first things that we're going to need we're going to have to import two fun or two Imports and the first one is going to be glob for Global and then import OS now the next thing we want to do is specify the directory uh that this stuff is located in now you can shorten the directories for example where file delete you can just say whole folder right so we could say whole folder oh my goodness can't type and then use forward slashes I think you can use back slashes as long as you use two I want to say but just use a forward slash it makes navigating a lot easier so then within whole folder let's say uh we want to go ahead and delete this text document so let me just make a copy of it and we're going to delete this one so um so we'll say that's our directory whole folder now the next thing we want to do is say os. CH dur we want to change directory in to that directory subsequently we want to specify the files that we want to delete so that'll be Global and then anything in that directory ending with txt let getting the axe for file name in files we want to do OS unlink file name unlink is short for dominate so uh let's go ahead and run this pop this over here and we see it actually deleted both of our uh text documents because we said to delete everything that was text but everything else is still intact so we didn't delete everything just the text files so now I know you know what everybody wants to know is how do we delete the entire directory you know do some serious destruction so um if you you might be thinking you could just maybe delete all from a whole folder but if you do that let me show you what ends up happening you get access denied we we don't have access to do that because this isn't like a valid windows program I think you could go into your user account settings and just take away the requirement for Access so it's kind of like whenever you download like a third party program and you go to run it and windows is like are you sure you know um that's not happening here instead we're just trying to go ahead and go through it and not ask for Access and it's getting dened so you could go through and remove that or you could actually delete uh hold directories this way so I'm going to show you guys the way to delete directories properly without making yourself super vulnerable plus it's going to take a lot less code now when you do this before we kind of use Shand we just said okay the folder because this is our file was located right here but let's um let's use we're going to use a different function and we're going to type out the entire thing just due to the nature of of this uh function here so we're going to import shutil and from shutil uh we want to get remove tree so this is going to remove the entire tree so the tree that we want to remove we're going to give the entire location because it's going to remove a tree and you're not really I mean you're kind of specifying the name but it's just good to go ahead and type out the entire thing whenever you're deleting files especially whole folders really so this is going to be users H uh desktop and we're we're hiding over here in sentex lessons in the file manip folder and we want to go ah and delete the entire whole folder and that's it it's a oneline function basically so you hit it you go goodbye whole folder it is now gone so that's pretty much it with deleting directories and files in the next tutorial I'm going to be showing you guys how to rename and to move files so kind of like deleting and um well deleting it besides from the uh directory it was kind of different functions but deleting is pretty much the same overall and then also renaming and moving is kind of the same thing so with that uh hopefully that's helping some of you guys stay tuned to the next video as always thanks for watching
Original Description
Sentdex.com
Facebook.com/sentdex
Twitter.com/sentdex
How to Delete files and directories with Python.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 44 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
▶
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Workflow Automation
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
Answer Calculator: Step-by-Step Math Help
Medium · Machine Learning
🎓
Tutor Explanation
DeepCamp AI