Average Joe
Average Joe

Reputation: 23

File handling in Python: being used by another process

Well i made this script that its supouse to logg some keystrokes for a while save them in a file and then erase the file if the user want to however when the script tryes to delete the file i get this error.

Traceback (most recent call last):File "C:\Users\Tormentor\Desktop\S.D.A.K.L\pregunta.py", line 34, in os.remove(path2+"\"+name) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:'C:\Users\Public\myfile.txt'

I made some research and i think that it cant be deleted because my "snp" function never closes the file where the keystrokes are logged so how can i close the file to delete it? Thanks for your help :).

import os
import time
import pyHook, pythoncom, sys, logging

path="C:\\Users\\Public\\myfile.txt"

path2="C:\\Users\\Public"

name="myfile.txt"

TinM=10

def snp(event):    #<---------- Not closing file ???
    global path
    logging.basicConfig(filename=path, level=logging.DEBUG, format='%(message)s')
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True


timeout=time.time()+TinM
while timeout > time.time():
    hooks_manager = pyHook.HookManager()
    hooks_manager.KeyDown = snp
    hooks_manager.HookKeyboard()
    print("Logging keystrokes")
    pythoncom.PumpWaitingMessages()
else:
    hooks_manager.UnhookKeyboard()
    x=input("Keylogger stoped do you want to delete the archive? y / n")
    if x == "y":
        for(path2,dirs,files) in os.walk(path2):
            if name in files:
                os.remove(path2+"\\"+name) # <----- This line triggers the error.
                print("Archive deleted. Goodbye")
            else:
                print("Archive does not exist or cant be found goodbye! :D")
    else:
        print("Goodbye! :D")

Upvotes: 2

Views: 7528

Answers (1)

mhawke
mhawke

Reputation: 87074

The file is being held open by your own process.

logging.basicConfig(filename=path, level=logging.DEBUG...

opens the file specified by filename. It does not close it until the process exits, or logging.shutdown() is called, so you could call shutdown() in your snp() function.

However, that requires that logging be initialised every time that a key is pressed, which is very inefficient. A better design would be to call logging.basicConfig() once in the main part of your script, and to call logging.shutdown() prior to removing the file. Your snp() function then becomes:

def snp(event):
    logging.log(logging.DEBUG, chr(event.Ascii))
    return True

and the main part of the script:

logging.basicConfig(filename=path, level=logging.DEBUG, format='%(message)s')
timeout=time.time()+TinM
while timeout > time.time():
    hooks_manager = pyHook.HookManager()
    hooks_manager.KeyDown = snp
    hooks_manager.HookKeyboard()
    print("Logging keystrokes")
    pythoncom.PumpWaitingMessages

hooks_manager.UnhookKeyboard()
logging.shutdown()
x=input("Keylogger stoped do you want to delete the archive? y / n")
if x == "y":
    for(path2,dirs,files) in os.walk(path2):
        if name in files:
            os.remove(path2+"\\"+name) # <----- This line triggers the error.
            print("Archive deleted. Goodbye")
        else:
            print("Archive does not exist or cant be found goodbye! :D")
else:
    print("Goodbye! :D")

Note that I also removed the else clause from the while statement because it is always executed for the code that you show.

Upvotes: 1

Related Questions