close

[Solved] IOError: [Errno 2] No such file or directory (when it really exist) Python

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error IOError: [Errno 2] No such file or directory (when it really exist) Python in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How IOError: [Errno 2] No such file or directory (when it really exist) Python Error Occurs?

Today I get the following error IOError: [Errno 2] No such file or directory (when it really exist) Python in python.

How To Solve IOError: [Errno 2] No such file or directory (when it really exist) Python Error ?

  1. How To Solve IOError: [Errno 2] No such file or directory (when it really exist) Python Error ?

    To Solve IOError: [Errno 2] No such file or directory (when it really exist) Python Error os.listdir() just returns bare filenames, not fully qualified paths. These files (probably?) aren't in your current working directory, so the error message is correct — the files don't exist in the place you're looking for them.

  2. IOError: [Errno 2] No such file or directory (when it really exist) Python

    To Solve IOError: [Errno 2] No such file or directory (when it really exist) Python Error os.listdir() just returns bare filenames, not fully qualified paths. These files (probably?) aren't in your current working directory, so the error message is correct — the files don't exist in the place you're looking for them.

Solution 1

You need to provide the actual full path of the files you want to open if they are not in your working directory :

import os
def send2():
    path = '/home/pi/Downloads/test/'
    arr = os.listdir(path)
    for x in arr:
        xpath = os.path.join(path,x)
        with open(xpath, 'rb') as fh:
            while True:
                # send in 1024byte parts
                chunk = fh.read(1024)
                if not chunk: break
                ser.write(chunk)

Solution 2

os.listdir() just returns bare filenames, not fully qualified paths. These files (probably?) aren’t in your current working directory, so the error message is correct — the files don’t exist in the place you’re looking for them.

Simple fix:

for x in arr:
    with open(os.path.join(path, x), 'rb') as fh:
        …

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read