Python Forum

Full Version: How to fix error code 2 in python, “directory not found”?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm setting up a keylogger for my mac OS to track esports reaction times. Most of the code is fine, with pynput installed successfully and the code seeming to check out, however when I go to run the code in Visual studio code (my text editor) i get the error:

'can't open file '.logger.pyw': [Errno 2] No such file or directory'

can anyone help me with this? i'm quite new to python so i'm sure its a simple fix i've not done!

My python code is:

from pynput.keyboard import Key, Listener
#vanilla
import logging

#make a log file
log_dir = ""

logging.basicConfig(filename=(log_dir + "key_log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s:')

def on_press(key):
    logging.info(str(key))
    #if key == Key.esc:
        #stop listener
        #return false

with Listener(on_press=on_press) as listener:
    listener.join()
And my terminal is:

PS /Users/dav3javu/Desktop/keylogger/pynput-master> python3 .logger.pyw                         
/Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app
/Contents/MacOS/Python: can't open file '.logger.pyw': [Errno 2] No such file or directory
Quote:'can't open file '.logger.pyw': [Errno 2] No such file or directory'
Always post complete, unaltered error traceback, inside of BBCode error tags. It contains valuable information that helps with debugging.
log_dir is an empty string, so not needed at all.
It also looks like the file is in the same directory as you python script.
If so, add the following code (not tested):
# replace line 8 with:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
logging.basicConfig(filename='key_log.txt', level=logging.DEBUG, format='%(asctime)s: %(message)s:')

# add after line 3
import os
also when running, use python3 .logger.py