Monday, April 21, 2025

Delete Empty Directory - Python

import os

os.chdir('c:\\Python38-64\\')

os.rmdir('EMPTY-DIR')

This code uses the os module to remove an empty directory called EMPTY-DIR in the directory c:\\Python38-64\\.

The os.chdir() function is used to change the current working directory to c:\\Python38-64\\, which is where the EMPTY-DIR directory is located.

The os.rmdir() function is then called with the argument 'EMPTY-DIR'. This function is used to remove a directory. However, it only works if the directory is empty. If the directory contains any files or subdirectories, the function will raise an error.

This code is useful for cleaning up empty directories created by a script or for removing unnecessary empty directories. It is important to note that os.rmdir() will only remove empty directories. If you want to remove a directory that contains files or subdirectories, you can use shutil.rmtree() instead. However, use caution when removing directories as the process is irreversible and could result in loss of data if not done carefully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...