471,349 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Access Denied while trying to delete file from python script

I am trying to write a script that deletes certain files based on
certain criteria.

What I am trying to do is to automate the process of deleting certain
malware files that disguise themselves as system files and hidden
files. When I use os.remove() after importing the os module is raises
a Windows Error: Access denied probably because the files have
disguised themselves as system files. Is there anway to get adequate
privileges under windows to be able to delete system files from within
a python script. I know it is probably a windows security feature but
is there anyway to gain such privileges.

Jul 14 '07 #1
2 8917
On Jul 14, 3:39 am, "Viewer T." <dtgead...@yahoo.comwrote:
I am trying to write a script that deletes certain files based on
certain criteria.

What I am trying to do is to automate the process of deleting certain
malware files that disguise themselves as system files and hidden
files. When I use os.remove() after importing the os module is raises
a Windows Error: Access denied probably because the files have
disguised themselves as system files. Is there anway to get adequate
privileges under windows to be able to delete system files from within
a python script. I know it is probably a windows security feature but
is there anyway to gain such privileges.
You'll need to use the PyWin32 package (AKA win32all). There's a
win32security module. These links might help:

http://mail.python.org/pipermail/pyt...ry/367441.html
http://www.thescripts.com/forum/thread28850.html
http://aspn.activestate.com/ASPN/doc...d_Locking.html

Sometimes you can beat windows over the head using its own delete
commands:

os.system('DEL /F /S /Q "%s"' % item)
os.system(r'RD /S /Q "%s"' % item)

I know there's a cookbook recipe on it, but I can't seem to find it
right now. Hopefully this will help you some though.

Mike

Jul 14 '07 #2
ky******@gmail.com wrote:
On Jul 14, 3:39 am, "Viewer T." <dtgead...@yahoo.comwrote:
>I am trying to write a script that deletes certain files based on
certain criteria.

What I am trying to do is to automate the process of deleting certain
malware files that disguise themselves as system files and hidden
files. When I use os.remove() after importing the os module is raises
a Windows Error: Access denied probably because the files have
disguised themselves as system files. Is there anway to get adequate
privileges under windows to be able to delete system files from within
a python script. I know it is probably a windows security feature but
is there anyway to gain such privileges.

You'll need to use the PyWin32 package (AKA win32all). There's a
win32security module. These links might help:

http://mail.python.org/pipermail/pyt...ry/367441.html
http://www.thescripts.com/forum/thread28850.html
http://aspn.activestate.com/ASPN/doc...d_Locking.html

Sometimes you can beat windows over the head using its own delete
commands:

os.system('DEL /F /S /Q "%s"' % item)
os.system(r'RD /S /Q "%s"' % item)

I know there's a cookbook recipe on it, but I can't seem to find it
right now. Hopefully this will help you some though.
I think what you want is this one:
http://aspn.activestate.com/ASPN/Coo.../Recipe/303343

I wrote/maintain an Win32 antivirus tool written in C and it uses the same Win32 functions to unset any hidden/system attributes on files before attempting to delete them. In the same vein, you'll find that some files are locked and unable to be deleted, so you're probably want to look into the following also:

* killing processes to remove currently running files
http://www.answermysearches.com/how-...-in-python/57/

* scheduling files for removal on reboot using MoveFileEx
(you'd have to use win32 extensions to access the MoveFileEx function)
http://msdn2.microsoft.com/En-US/library/aa365240.aspx

Hope that helps,

-Jay
Jul 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by calfdog | last post: by
1 post views Thread by paritycheck | last post: by
6 posts views Thread by ASP.Confused | last post: by
reply views Thread by ASP.Confused | last post: by
5 posts views Thread by wo20051223 | last post: by
2 posts views Thread by W. eWatson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.