[BangPypers] remove svn checkout folder
Sateesh Kumar
sateeshpyper at gmail.com
Fri Oct 28 06:35:34 CEST 2011
On Fri, Oct 21, 2011 at 12:37 PM, Shashidhar Paragonda <
shashidhar85 at gmail.com> wrote:
* Then i tried *shutil.rmtree(path/to/folder/) but here I am
> facing permission denied error
>
> code : os.chmod(path/to/folder, 0777)
> if os.access(path/to/folder, os.R_OK):
> shutil.rmtree(path/to/folder/)
> is there any problem in this code or which is best solution for the
> above requirement.
>
What you are doing is changing the permission of the folder, but if
there are files inside that folder which have the 'Read-Only'
attribute set then their removal would fail and an exception
thrown. If you examine the stack trace you can find the file whose
removal attempt failed.
shutil.rmtree can take a function as a callback and you can make use
of that feature to handle failures in file/subfolder removal and to remove
them. Such an approach is explained here:
http://stackoverflow.com/questions/1213706/what-user-do-python-scripts-run-as-in-windows
http://stackoverflow.com/questions/2656322/python-shutil-rmtree-fails-on-windows-with-access-is-denied
Also when you are passing windows path as an argument to Python
functions it is better to pass them as raw strings[1]. Your invocation
should be
in the form:
os.listdir(r'c:\temp')
and so on.
HTH,
sateesh
1. http://docs.python.org/tutorial/introduction.html (Section 3.1.2 Strings)
More information about the BangPypers
mailing list