Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

defsnp(event):
    logging.log(logging.DEBUG, chr(event.Ascii))
    returnTrue

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) inos.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.

Post a Comment for "File Handling In Python: Being Used By Another Process"