Python Forum
Getting "folder in use" error from OS after I run my program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting "folder in use" error from OS after I run my program
#1
Hello all,

I'm relatively new to Python, and I'm enjoying the learning process and the info I find here!

I am writing a little program that will delete all contents of a particular folder. I am using a test folder of "c:\py" (yes, windows) and the program works, but afterward when I go to my file explorer window and try to manually delete the now-empty folder a pop-up says "Folder in use - The action can't be completed because the folder or a file in it is open in another program."

I am using IDLE, and if I restart the shell I can then remove the folder just fine. This tells me that something hasn't been gracefully closed even after the program completes. I tried to see if there is an os.close() function, and there is, but that needs a file descriptor that I am unsure about. There are no open file handles that I know of.

(And if you wonder why I don't just use rmtree() on the folder itself the point of my program is to delete the contents of the folder but leave the folder itself now that it's been cleaned. I am just trying to remove it afterwards because I want to copy over a backup I made of it previously.)

Here is my code. What am I doing wrong?

!!!Heed the warning in the comments if you decide to try to run this. It WILL delete everything if you have a a c:\py folder!!!


#This will remove files and directories in "path". ***Be careful!!***

import os, shutil

path = "c:\\Py"                    #Root path of dir to clean - caution!
os.chdir(path)                     #Switch to that directory to operate on
directories = []                   #List to hold the names of the directories to remove

for x in os.scandir(path):         #Read the contents of the directory
    if x.is_file():                #Check for files
        os.remove(x.name)          #Clean out all files in root path
    elif x.is_dir():               #Check for directories
        directories.append(x.name) # Build a list of dirs to be removed next

for n in directories:              #Go through the complete list of dirs to be removed
    shutil.rmtree(os.path.join(path, n)) #This will append the dir name to the path and remove it
Reply
#2
I guess it is the os.chdir(path). Your program's current directory is the folder that you want to remove, and this prevents you from removing the folder. Try os.chdir(os.path.expanduser('~')) at the end of the file (this is your home directory).
Reply
#3
You are correct, thank you! I had a feeling this was it, but I felt changing dirs for a "delete" program was somewhat dangerous. But doing so in the last line will not do anything drastic.

Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 524 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,114 Jun-21-2022, 11:11 PM
Last Post: Larz60+
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,469 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,465 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,033 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  program error Dalpi 4 2,458 Apr-30-2020, 11:46 PM
Last Post: Dalpi
  Program gives error but doesn't say where the error is. FWendeburg 3 2,791 Mar-09-2019, 11:42 PM
Last Post: FWendeburg
  Delete directories in folder is not working after folder is updated asheru93 2 2,646 Feb-13-2019, 12:37 PM
Last Post: asheru93
  copy content of folder to existing folder shlomi27 0 2,627 Aug-11-2018, 01:44 PM
Last Post: shlomi27
  Syntax Error in Program bijay1983 2 3,000 Dec-24-2017, 05:36 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020