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
  write code that resides in parent directory franklin97355 0 64 Today, 12:03 AM
Last Post: franklin97355
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,602 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  I found a problem with Python Lahearle 12 1,396 Jul-20-2023, 10:58 PM
Last Post: Pedroski55
  Syntax error while executing the Python code in Linux DivAsh 8 1,454 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Coding error. Can't open directory EddieG 6 1,063 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,085 Jun-27-2023, 01:17 PM
Last Post: diver999
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,492 Mar-27-2023, 07:38 AM
Last Post: buran
  Module Not Found Error bitoded 4 1,346 Jan-01-2023, 09:08 AM
Last Post: bitoded
  Error in if-then-else python code Led_Zeppelin 6 2,287 Jul-27-2022, 07:53 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