Home > Back-end >  Python saying "FileNotFoundError: [Errno 2] No such file or directory: 'script.vbs'&q
Python saying "FileNotFoundError: [Errno 2] No such file or directory: 'script.vbs'&q

Time:12-17

Python is giving me the error "FileNotFoundError: [Errno 2] No such file or directory: 'script.vbs'" but the file is in the same directory that it's looping through.

Script:

import os
path = r"C:\Users\bruh\Downloads\Directory"

directory = os.listdir(path)
for file in directory:
    print(file)
    open(file)

By the way, it literally prints the file name and then says it cant find it.

CodePudding user response:

It prints the file name, but not the path. You need to add the path:

    open( os.path.join( path, file ) )

CodePudding user response:

import os
path = r"C:\Users\bruh\Downloads\Directory"
# check current working dir
cwd = os.getcwd()
print(cwd)
# change you os dir to path
os.chdir(r'C:\Users\bruh\Downloads\Directory')
for i in os.listdir():
    print(i, end='\n')
    open(i)
  • Related