Python IOError exception when creating a long file

I get an IOError shown below when trying to open a new file using "open (fname, 'w+')". The complete error message is below.

The file does not exist, but I verified using "os.access(dir_name, os.W_OK)" and "os.path.exists (dir_name)" that the parent directory for the file does exist.

I am wondering if the file name is just too long for Windows, or if I am doing something wrong. Any tips would be appreciated. Thank you very much.

Error message:

IOError: [Errno 2] No such file or directory: 'C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js'

In the Windows API the maximum path length is limited to 260 characters.

http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx

Update: prepend "\\?\" to the path.

Issue 30641: No way to specify "File name too long" error in except , There appears to be an errno for file name too long, so I presume you are making a feature request for a new exception class. I believe Antoine� IOError ExceptionIt is an error raised when an input/output operation fails, such as the print statement or the open() function when trying to open a file that

Here is some related code which works for me (I have very long file names and paths):

for d in os.walk(os.getcwd()):
    dirname = d[0]
    files = d[2]
    for f in files:
        long_fname = u"\\\\?\\" + os.getcwd() + u"\\" + dirname + u"\\" + f
        if op.isdir(long_fname):
            continue
        fin = open(long_fname, 'rb')
        ...

Note that for me it only worked with a combination of all of the following:

  1. Prepend '\\?\' at the front.

  2. Use full path, not relative path.

  3. Use only backslashes.

  4. In Python, the filename string must be a unicode string, for example u"abc", not "abc".

Also note, for some reason os.walk(..) returned some of the directories as files, so above I check for that.

6. Built-in Exceptions — Python 2.7.18 documentation, Some built-in exceptions (like IOError ) expect a certain number of The filename attribute is None when this exception is created with other than 3 arguments. option, or the WANT_SIGFPE_HANDLER symbol is defined in the pyconfig.h file. This cannot occur for long integers (which would rather raise� Assertions in Python. An assertion is a sanity-check that you can turn on or turn off when you are done with your testing of the program. The easiest way to think of an assertion is to liken it to a raise-if statement (or to be more accurate, a raise-if-not statement).

You can monkey patch the tarfile module with this:

import tarfile

def monkey_patch_tarfile():
    import os
    import sys
    if sys.platform not in ['cygwin', 'win32']:
        return
    def long_open(name, *args, **kwargs):
    # http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#maxpath
        if len(name) >= 200:
            if not os.path.isabs(name):
                name = os.path.join(os.getcwd(), name)
            name = "\\\\?\\" + os.path.normpath(name)
        return long_open.bltn_open(name, *args, **kwargs)
    long_open.bltn_open = tarfile.bltn_open
    tarfile.bltn_open = long_open

monkey_patch_tarfile()

Chapter 11: Files and exceptions, When there are a large number of files, they are often organized into directories ( also called "folders"). IOError: [Errno 2] No such file or directory: 'test.cat' Since Python is specifically designed to process text files, it provides methods that make the job easy. Whenever a runtime error occurs, it creates an exception. When raising (or re-raising) an exception in an except or finally clause __context__ is automatically set to the last exception caught; if the new exception is not handled the traceback that is eventually displayed will include the originating exception(s) and the final exception.

If it's not the length of the filename, it's the contents of the filename...

Python is treating '\12' as a control sequence.

>>> fn='C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js'
>>> print fn
C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12bcde_corporate_nov_12.projectbcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks
94897740\json.data\Link\Link Utilizationnalyzer393146160-data0.js

Using raw strings for Windows filenames will help:

>>> fn=r'C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js'
>>> print fn
C:\Documents and Settings\Administrator\op_models\Corp_Network_Nov12\abcde_corporate_nov_12.project\abcde_corporate_nov_12-ctr.rptd.dir\ctr\Non Business Hours for Weeknights\hourly_data_for_2_weeks\1294897740\json.data\Link\0\Link Utilization\analyzer393146160-data0.js

Update

Alternatively, use forward slashes '/' instead of backslashes '\', since these will work on all operating systems and will save you hassles with backslashes right at the end of a pathname as in your comments.

See also os.path.join() .

Update 2

Simplified demonstration of problem:

>>> open('.\12\n\r\file.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '.\n\n\r\x0cile.txt'
>>> open('./12/n/r/file.txt')
<open file './12/n/r/file.txt', mode 'r' at 0x7ff83f98>

C:\Users\johnysweb>copy .\12\n\r\file.txt con
Blah
        1 file(s) copied.

The Exceptions Class — XlsxWriter Documentation, This exception is raised if there is a file permission, or IO error, when writing the For Python 3 use input() instead of raw_input(). decision = raw_input("Exception As noted in the exception message, files larger than 4GB can be created by is raised during Workbook add_worksheet() if a worksheet name is too long or� The following are 8 code examples for showing how to use exceptions.IOError().These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

Check the length of the entire path and then append the necessary Windows long path format. It should be noted that this doesn't work for accessing data from remote directories i.e. paths that begin with '\\some_remote_location\..' so you will need to map that directory locally in order to get "long path" to work.

if len(path_and_file) > 250: #I think the max is 260 but I left a buffer :)
    path_and_file = '\\\\?\\'+path_and_file

Python Tutorial: Exception Handling, Exception Handling in Python It's possible to create "custom-made" exceptions: With the raise statement it's possible to I/O error(2): No such file or directory. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

How to Best Use Try-Except in Python, And learn to use Python exception handling with code examples. way of handling unexpected errors along with many more exception handling constructs . an error. f = open("no-file") except IOError as err: # Creating IOError instance for� import os THIS_FOLDER = os. path. dirname (os. path. abspath (__file__)) my_file = os. path. join (THIS_FOLDER, 'myfile.txt') code like this, based on deriving the current path from Python's magic __file__ variable, will work both locally and on the server, both on Windows and on Linux

(Tutorial) Exception and Error Handling in Python, Errors cannot be handled, while Python exceptions can be handled at the run time. Loading a very large data file; Running a Machine Learning/Deep For each method call, one stack frame will be created, and local as� If no exception occurs then code under except clause will be skipped. If file don't exists then exception will be raised and the rest of the code in the try block will be skipped; When exceptions occurs, if the exception type matches exception name after except keyword, then the code in that except clause is executed.

Python 3 - Exceptions Handling, Python 3 - Exceptions Handling - Python provides two very important features to handle except IOError: print ("Error: can\'t find file or read data") else: print (" Written content You cannot use else clause as well along with a finally clause. Python also allows you to create your own exceptions by deriving classes from the� Creating Custom Exceptions. In the previous section, we. were focused on handling exceptions using the exceptions that are built-in to Python. However, as you are developing your application, you will most likely encounter situations where you want to handle exceptions a bit differently.

Comments
  • Thank you. The limit does go over 260 characters. The MSDN article mentions that we can use "\\?\" prefix for long file name, just curious if anyone knows how I can add the prefix to the file name. When I try to do a simple "+" operation, I get an error EOL while scanning single-quoted string'. (fname = '\\?\' + fname)
  • Use "\\\\?\\". A raw string won't work in this case.
  • Use forward slashes instead. Also see stackoverflow.com/faq#howtoask
  • I tried using the "\\\\?\\" and "\\\\UNC\\?\\" by appending them to the file name and got the same exception. Any which way, when I try to create a file in windows explorer, it would not let me create the file (windows blocks me from adding characters). I guess this is a system limitation?
  • ... combined with the very confusing error value; it should be ENAMETOOLONG, not ENOENT. I think this is one of those obscure Windows bugs that everyone is confused by at least once at some point in their career.
  • What about non-english languages? I feel that the maximum is lower.
  • Thanks a lot for mentioning that the Python string must be a Unicode string - that saved me!
  • The os.getcwd() fixed it for me. Has to be the whole path, not just a relative path.
  • You're better off consistently using forward slashes; they work fine in Windows and it results in code that's much more likely to work on other platforms.
  • The OP used a correctly encoded file path. Otherwise the error message would have been different (TypeError).
  • @cgohlke: Please see my second update which demonstrates the IOError exception with the same text as the OP reported.
  • I do think the encoding is correct, since I just tried a different file name (sm.txt with the whole path) that keeps me within the 260 character limit, and that did work without an exception. I do see the file being created.
  • @Johnsyweb: I don't doubt that one can get an IOError using incorrectly encoded path names. However, the path name used by the OP contains a NULL byte "\0" and this will lead to a TypeError in the open function. It is also unlikely that the 260+ character path name was typed manually by the OP. More likely it was the result of a function call.