How to remove read-only attrib directory with Python in Windows? -


i have read directory copied version controlled directory locked. enter image description here

when tried remove directory shutil.rmtree(test_objects_dir) command, got following error message.

windowserror: [error 5] access denied: 'c:\...\environment.txt' 
  • q : how can change attribute of in whole directory structure?

if using shutil.rmtree, can use onerror member of function provide function takes 3 params: function, path, , exception info. can use method mark read files writable while deleting tree.

import os, shutil, stat  def on_rm_error( func, path, exc_info):     # path contains path of file couldn't removed     # let's assume it's read-only , unlink it.     os.chmod( path, stat.s_iwrite )     os.unlink( path )  shutil.rmtree( test_objects_dir, onerror = on_rm_error ) 

now, fair, error function called variety of reasons. 'func' parameter can tell function "failed" (os.rmdir() or os.remove()). here depends on how bullet proof want rmtree be. if it's case of needing mark files writable, did above. if want more careful (i.e. determining if directory coudln't removed, or if there sharing violation on file while trying delete it), appropriate logic have inserted on_rm_error() function.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -