Access Denied while trying to delete file from python script

Discussion in 'Python' started by Viewer T., Jul 14, 2007.

  1. Viewer T.

    Viewer T. Guest

    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.
    Viewer T., Jul 14, 2007
    #1
    1. Advertising

  2. Viewer T.

    Guest

    On Jul 14, 3:39 am, "Viewer T." <> wrote:
    > 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/python-list/2006-February/367441.html
    http://www.thescripts.com/forum/thread28850.html
    http://aspn.activestate.com/ASPN/docs/ActivePython/2.5/pywin32/Windows_NT_Files_.2d.2d_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, 2007
    #2
    1. Advertising

  3. Viewer T.

    Jay Loden Guest

    wrote:
    > On Jul 14, 3:39 am, "Viewer T." <> wrote:
    >> 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/python-list/2006-February/367441.html
    > http://www.thescripts.com/forum/thread28850.html
    > http://aspn.activestate.com/ASPN/docs/ActivePython/2.5/pywin32/Windows_NT_Files_.2d.2d_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/Cookbook/Python/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-to-kill-a-process-by-name-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
    Jay Loden, Jul 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    1
    Views:
    2,882
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Oct 14, 2004
  2. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Access to the path is denied - trying to write a file

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Dec 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    28,231
    Steven Cheng[MSFT]
    Dec 16, 2005
  3. Replies:
    2
    Views:
    4,358
    mausingh
    Nov 19, 2010
  4. Craig Lee

    Access denied while trying QI on a DCOM object

    Craig Lee, Feb 28, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    147
    Craig Lee
    Feb 28, 2004
  5. Carl Graff
    Replies:
    0
    Views:
    166
    Carl Graff
    Oct 13, 2006
Loading...

Share This Page