Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

delete files inside folder python

import os
import glob

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

python delete folder and contents

import shutil
shutil.rmtree("dir-you-want-to-remove")
Comment

Python Removing Directory or File

>>> os.listdir()
['new_one', 'old.txt']

>>> os.remove('old.txt')
>>> os.listdir()
['new_one']

>>> os.rmdir('new_one')
>>> os.listdir()
[]
Comment

python how to delete a directory with files in it

import shutil

dir_path = '/tmp/img'

try:
    shutil.rmtree(dir_path)
except OSError as e:
    print("Error: %s : %s" % (dir_path, e.strerror))
Comment

How to delete a file or folder in Python?

os.remove() removes a file.

os.rmdir() removes an empty directory.

shutil.rmtree() deletes a directory and all its contents.
Comment

PREVIOUS NEXT
Code Example
Python :: list count python 
Python :: create a dictionary from index and column pandas 
Python :: python strip() 
Python :: range in python 
Python :: How to add all the numbers of a list using python? 
Python :: how to download a pip package with python and os 
Python :: python upload file to s3 
Python :: python message 
Python :: generate python 
Python :: Smart Weather Information App Using Python 
Python :: pandas join dataframe 
Python :: permutation and combination program in python 
Python :: python how to create a function 
Python :: python basic programs 
Python :: why is c++ faster than python 
Python :: pandas use dict to transform entries 
Python :: Python NumPy expand_dims Function Example 
Python :: how to create a new dataframe in python 
Python :: aws lambda logging with python logging library 
Python :: sorted 
Python :: python interview questions and answers pdf 
Python :: keras transfer learning 
Python :: remove all occurences 
Python :: object oriented python 
Python :: kwargs in python 
Python :: python convert np datetime to string 
Python :: print file in python 
Python :: Python RegEx Subn – re.subn() 
Python :: 3d graph python 
Python :: python image heatmap 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =