2

I want to create a file within tmpfs (under CentOS 6.5) like this:

fpath = '/tmpfs_mounted/with/long/file/name'
with open(fpath, 'w') as fd:
    write(somedata)
...

But I got the IOError: [Errno 36] File name too long: ... error, How to fix it?

coanor
  • 3,746
  • 4
  • 50
  • 67
  • which os you are using..? Linux or windows ..? – Rajiv Sharma Jun 23 '16 at 03:43
  • Possible duplicate of [Why does Python give "OSError: \[Errno 36\] File name too long" for filename shorter than filesystem's limit?](http://stackoverflow.com/questions/34503540/why-does-python-give-oserror-errno-36-file-name-too-long-for-filename-short) – Rajiv Sharma Jun 23 '16 at 03:45
  • @cyclops: I have browsed the thread, but it seems not the case with me. – coanor Jun 23 '16 at 04:02
  • Maybe make the jump to the file in two changes of directory, then using a relative path? So `import os` , `os.chdir('/up_to_some_dir/') os.chdir('./going_further/'), fpath='./local_dir/file_name'` I know it's not ideal, and you might want to check for Exceptions thrown so you don't end up changing data in a higher-level directory, but it might get the job done. – curious_weather Jun 23 '16 at 05:48

1 Answers1

1

Ok, I got it. Linux really have a base-name limit for 256 bytes, see here for a full list of all limitations. A simple code can verify that:

# -*- encoding:utf8 -*-
import os

if __name__ == '__main__':
    base = 'x'
    basename = ''
    while 1:
        basename += base
        try:
            with open(basename, 'w') as fd:
                os.remove(basename)
        except Exception as ex:
            print('length %d failed' % len(basename))
            break

I encounter the problem when I copy GBK encoded file name into Tmpfs, and I transferred the GBK filenames into UTF8, then the length changed:

>>> s = u'中'
>>> len(s.encode('gbk'))
>>> 2
>>> len(s.encode('utf8'))
>>> 3

so, the utf8-filenames may exceed 255 bytes.

coanor
  • 3,746
  • 4
  • 50
  • 67