3

something weird is happening.
In short explanation - my .py file gets deleted automatically when I use pynput lib


Detailed explanation - I create a file called keylog.py
I have installed ->pip install pynput,
when I run the code:

import logging

logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
 
def onPress(key):
    logging.info(str(key))

Everything is ok.


But when I add:
# from pynput.keyboard import Key, Listener
import logging
 
logging.basicConfig(filename=("keylog.txt"), level=logging.DEBUG, format=" %(asctime)s - %(message)s")
 
def onPress(key):
    logging.info(str(key))
 
# with Listener(on_press=onPress) as listener:
#    listener.join()

Even if the pynput import is commented out the keylog.py file gets deleted automatically.
I have no idea why this happen, I tried to delete PyCharm and re-install, I tried working in VScode, same thing happening.
I didn't configure something special.
I would like some help and explanation why this happen, Thanks.

yaron
  • 73
  • 5
  • 1
    I can't really reproduce the error, tried your second code snippet (without the comments of course) and it works flawlessly for me. – Łukasz Kwieciński Mar 04 '22 at 11:33
  • This seems very unlikely. Have you reproduced the error using just the code you have shared here? Please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – ChrisGPT was on strike Mar 04 '22 at 11:37
  • 1
    Does your code accidentally specify the logging filename as `keylog.py` instead of `keylog.txt`? – evergreen Mar 04 '22 at 12:08
  • hey @Chris, I'm not sure how you guide helps me. I cant debug because the file Delete it self even before I Ctrl+S. even if I saved it, as soon as I try to run the file, I get error that file is missing and then the file disappears. – yaron Mar 04 '22 at 15:14
  • Hey @evergreen, nope. I tried changing the name of the file to hello.py and still deletes.. – yaron Mar 04 '22 at 15:14
  • I just encountered this problem. Windows 10's security basically put all my folder in "read-only" mode because it thinks it detected a keylogger threat... That's why the files disappear or can't be saved. – Guimoute Nov 18 '22 at 00:15

2 Answers2

2

Found the issue if anyone in the future will have the same problem. when I opened a project for some reason it didn't ask me if I want to trust this project. therefore, the windows defender sees pynput as maybe malicious so it blocked these files, until I made them trusted.

yaron
  • 73
  • 5
  • 1
    Quick question how have you done that? I am encountering the same problem and the only options in Windows Defender are "remove" and "quarantine"... I will search a little more of course – D_00 Jun 27 '22 at 07:22
  • Well I just randomly deleted a line in the thread and it apparently solved that issue.? – D_00 Jun 27 '22 at 07:26
0

Basically pynput is used to record keystrokes which is termed as Malicious in Windows Security. Try Disabling it!

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 14 '22 at 16:12