python 中的 os 模块说 "No such file or directory" 即使确实有一个

os module in python says "No such file or directory" even when there definently is one

我正在尝试打开一个 .npy 文件。但是,python 一直说不存在这样的文件或目录,即使有...

为了确保给出正确的路径名不是问题,我将目录更改为包含我想要的 .npy 文件的文件夹,然后使用 list.dir() 和用它来使用np.load(代码在下面)

os.chdir(filename_dir) #filename_dir is the directory I want to get to that contains the npy file I want)
the_path=os.path.join(os.getcwd()+os.listdir()[-1]) #i.e. I did this to make sure that the directory is correct
data=np.load(the_path)

但是,我得到了错误,[Errno 2] No such file or directory: ....... 当我尝试 np.load 时出现错误。我无法理解这一点,因为我明确地根据 python 所说的 exsists

创建了路径 the_path

提前感谢您的帮助!

:)

尝试添加 r。例如 file_path = r"文件路径"

这可能有用..

我认为这是问题所在...

Windows(默认情况下)的最大路径长度为 260 个字符。此文件的绝对路径超出了该限制。仅文件名就有 143 个字符。

看起来即使您尝试使用相对路径访问文件(即 chdir() 到适当的文件夹然后仅指定文件名),numpy 也可能正在计算绝对路径然后失败。

您说过将文件重命名为更短的名称可以解决问题,但由于您需要处理的文件数量众多,因此不切实际。

有一个 Windows 注册表项可以修改以启用长路径名:Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem\LongPathsEnabled

改变可能会有帮助。

或者你可以买 Mac