【发布时间】:2020-06-27 04:54:10
【问题描述】:
我在使用 numpy 的 savetxt 函数时遇到了问题。我一直在尝试将数据保存在文件结构的深处。下面是一个最小的工作示例(但是,要运行它,您需要创建以下目录)。
import numpy as np
dir1 = "LongDirectoryName/"
dir2 = "VeryLongDirectoryName/"
dir3 = "EvenLongerDirectoryName/"
dir4 = "LongestOfThemAllDirectoryName/"
filename = "../" + dir1 + dir2 + dir3 + dir4 + "longfilename_with_129_charss.txt" # this works
#filename = "../" + dir1 + dir2 + dir3 + dir4 + "longfilename_with_130_charsss.txt" # this does not
print(len(filename))
myarray = np.array([1,2,3])
np.savetxt(filename, myarray)
相对文件路径不是问题,因为 129 个字符的文件名确实有效。在我看来,129 个字符是限制。当我去尝试 130 个字符的文件名时,我收到以下错误:
File "C:\Users\njkro\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1359, in savetxt
open(fname, 'wt').close()
FileNotFoundError: [Errno 2] No such file or directory: '../LongDirectoryName/VeryLongDirectoryName/EvenLongerDirectoryName/LongestOfThemAllDirectoryName/longfilename_with_130_charsss.txt'
其他信息
我使用的是 Windows 10。
我用的是Anaconda,我用的numpy版本是1.16.2。
我的问题:
- 谁能证实这一点?
- 这个字符限制有解释吗?我会理解 128 个字符的限制超过 129 个。
- 如果我无法更改文件系统,是否有解决方法(针对长文件名)?
【问题讨论】:
-
当我尝试它时效果很好(是的,我确实使用了注释掉的版本,而不是“这个有效”的版本)。
-
只是文件名 130 字符,还是整个路径?问题可能是整个文件路径的长度大于 Windows 的 MAX_PATH。 docs.microsoft.com/en-us/windows/win32/fileio/….
-
我回答了;您是否介意将问题的标题更改为:“Windows 中 Python 中长路径名上的 FileNotFoundError”?错误发生在
open()函数,所以和numpy无关。
标签: python