close

[Solved] open file in “w” mode: IOError: [Errno 2] No such file or directory

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error open file in “w” mode: IOError: [Errno 2] No such file or directory 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 open file in “w” mode: IOError: [Errno 2] No such file or directory Error Occurs?

Today I get the following error open file in “w” mode: IOError: [Errno 2] No such file or directory in python.

How To Solve open file in “w” mode: IOError: [Errno 2] No such file or directory Error ?

  1. How To Solve open file in “w” mode: IOError: [Errno 2] No such file or directory Error ?

    To Solve open file in “w” mode: IOError: [Errno 2] No such file or directory Error I had the same error, but in my case the cause was, under Windows, a path longer than ~250 characters.

  2. open file in “w” mode: IOError: [Errno 2] No such file or directory

    To Solve open file in “w” mode: IOError: [Errno 2] No such file or directory Error I had the same error, but in my case the cause was, under Windows, a path longer than ~250 characters.

Solution 1

You’ll see this error if the directory containing the file you’re trying to open does not exist, even when trying to open the file in w mode.

Since you’re opening the file with a relative path, it’s possible that you’re confused about exactly what that directory is. Try putting a quick print to check:

import os

curpath = os.path.abspath(os.curdir)
packet_file = "%s/%s/%s/%s.mol2" % ("dir", "dir2", "dir3", "some_file")
print "Current path is: %s" % (curpath)
print "Trying to open: %s" % (os.path.join(curpath, packet_file))

packetFile = open(packet_file, "w")

Solution 2

I had the same error, but in my case the cause was, under Windows, a path longer than ~250 characters.

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