Python Forum
How to fix error code 2 in python, “directory not found”?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix error code 2 in python, “directory not found”?
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  module not found error Pedroski55 15 178 Jun-10-2026, 05:41 PM
Last Post: noisefloor
  Upgraded Python: Module no longer found - for Linux Curbie 8 3,662 Mar-05-2025, 06:01 PM
Last Post: Curbie
Question [SOLVED] Upgraded Python: Module no longer found Winfried 1 2,894 Jan-01-2025, 02:43 PM
Last Post: Larz60+
  Module not found error even though installed NZGeorge 1 8,180 Jul-10-2024, 09:08 AM
Last Post: Larz60+
  write code that resides in parent directory franklin97355 3 2,264 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  file open "file not found error" shanoger 8 17,397 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 5,456 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  I found a problem with Python Lahearle 12 4,909 Jul-20-2023, 10:58 PM
Last Post: Pedroski55
  Syntax error while executing the Python code in Linux DivAsh 8 8,733 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Coding error. Can't open directory EddieG 6 3,260 Jul-13-2023, 06:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020