Python IOError exception while creating a long file

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

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

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

Error message:

IOError: [Errno 2] There is 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 \ CMS \ Non Work time \ Hourly_weata_we9_48 json.data \ Link \ 0 \ Link Use \ analyzer393146160-data0.js'

+8
python filenames ioerror
source share
4 answers

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: Add "\\? \" To the path.

+7
source share

You can decapitate the tarfile module as follows:

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() 
+4
source share

Here is some code that 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') ... 

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

  • Prepare '\\? \ 'front.

  • Use the full path, not the relative path.

  • Use only backslashes.

  • In Python, the file name string should be a unicode string, for example u "abc", not "abc".

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

+3
source share

If this is not the length of the file name, this is the contents of the file name ...

Python treats "\ 12" as an escape 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 file names 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

As an alternative, use the slashes' / 'instead of backslashes \' as they will work on all operating systems and will save your backslash problems right at the end of the path, as in your comments.

See also os.path.join() .

Update 2

Simplified demonstration of the 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. 
0
source share

Source: https://habr.com/ru/post/651015/


All Articles