I have the following code:
os.remove('_Temp_Dir_\main' + str(i) + '.exe')
os.rmdir('_Temp_Dir_')
Which gives me:
OSError: [WinError 145] Directory is not empty: '_Temp_Dir_'
if i put the line
time.sleep(0.05)
before os.rmdir()
, it works correctly. I think os.remove()
isn't fast enough to remove the file. Any way to wait for it to finish its work?
Use shutil.rmtree()
to remove the directory and don't bother with removing the file:
import shutil
shutil.rmtree('_Temp_Dir_')
The os.remove()
works just fine (it won't return until the file remove completes), there must be other files in that directory that the process left behind and are removed during your sleep()
call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With