Pathname too long to open?

Viewed 23234

This is a screenshot of the execution:

Enter image description here

As you see, the error says that the directory "JSONFiles/Apartment/Rent/dubizzleabudhabiproperty" is not there.

But look at my files, please:

Enter image description here

The folder is definitely there.

Update 2

The code

self.file = open("JSONFiles/"+ item["category"]+"/" + item["action"]+"/"+ item['source']+"/"+fileName + '.json', 'wb') # Create a new JSON file with the name = fileName parameter
        line = json.dumps(dict(item)) # Change the item to a JSON format in one line
        self.file.write(line) # Write the item to the file

UPDATE

When I change the file name to a smaller one, it works, so the problem is because of the length of the path. what is the solution please?

4 Answers

below is Python 3 version regarding @Eryk Sun's solution.

def winapi_path(dos_path, encoding=None):
    if (not isinstance(dos_path, str) and encoding is not None): 
        dos_path = dos_path.decode(encoding)
    path = os.path.abspath(dos_path)
    if path.startswith(u"\\\\"):
        return u"\\\\?\\UNC\\" + path[2:]
    return u"\\\\?\\" + path

#Python 3 renamed the unicode type to str, the old str type has been replaced by bytes. NameError: global name 'unicode' is not defined - in Python 3

Adding the solution that helped me fix a similar issue: python version = 3.9, windows version = 10 pro.

I had an issue with the filename itself as it was too long for python's open built-in method. The error I got is that the path simply doesn't exist, although I use the 'w+' mode for open (which is supposed to open a new file regardless whether it exists or not).

I found this guide which solved the problem with a quick change to window's Registry Editor (specifically the Group Policy). Scroll down to the 'Make Windows 10 Accept Long File Paths' headline.

Don't forget to update your OS group policy to take effect immediately, a guide can be found here.

Hope this helps future searches as this post is quite old.

Related