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 SettingsAdministratorop_modelsCorp_Network_Nov12abcde_corporate_nov_12.projectabcde_corporate_nov_12-ctr.rptd.dirctrNon Business Hours for Weeknightshourly_data_for_2_weeks1294897740json.dataLinkLink Utilizationanalyzer393146160-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.


你可以用这个来修补tarfile模块:

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()

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:

  • Prepend '?' at the front.

  • Use full path, not relative path.

  • Use only backslashes.

  • 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.

    链接地址: http://www.djcxy.com/p/54688.html

    上一篇: 在Python中捕获IOError

    下一篇: 创建长文件时出现Python IOError异常