Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

delete contents of directory python

import os
import glob

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

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

delete folders using python

import shutil
shutil.rmtree(r'Path where the folder with its files is storedFolder name')
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

Python delete directory contents

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))
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 :: run code in python atom 
Python :: most frequent word in an array of strings python 
Python :: list variables in session tensorflow 1 
Python :: python face recognition 
Python :: Python code for checking if a number is a prime number 
Python :: circular list python 
Python :: rest_auth pip 
Python :: python if not null or empty 
Python :: how to set variable in flask 
Python :: make a gif with images python 
Python :: find sum of factors of a number python 
Python :: how to run terminal commands in python 
Python :: how to make a list a string 
Python :: pytube sample script 
Python :: Return the number of times that the string "hi" appears anywhere in the given string. python 
Python :: dataframe color cells 
Python :: excute a command using py in cmd 
Python :: embed image in html from python 
Python :: how to use argparse 
Python :: Python program to print even numbers in a list 
Python :: how to find the closest value in column python 
Python :: Python Roman to Integer method 2 
Python :: pandas column rank 
Python :: django q objects 
Python :: python run in another thread decorator 
Python :: asyncio run 
Python :: print schema in pandas dataframe 
Python :: edit pandas row value 
Python :: python datetime 
Python :: Use module Crypto.Cipher.PKCS1_OAEP instead 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =