python-numpy FileNotFoundError:[错误2]没有这样的文件或目录 [英] python - numpy FileNotFoundError: [Errno 2] No such file or directory

查看:134
本文介绍了python-numpy FileNotFoundError:[错误2]没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

import os.path
import numpy as np
homedir=os.path.expanduser("~")
pathset=os.path.join(homedir,"\Documents\School Life Diary\settings.npy")
if not(os.path.exists(pathset)):
    ds={"ORE_MAX_GIORNATA":5}
    np.save(pathset, ds)

但是他给我的错误是:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Maicol\\Documents\\School Life Diary\\settings.npy'

我该如何解决?文件夹未创建...

How can I solve this? The folder isn't created...

谢谢

推荐答案

好像您正在尝试将文件写入不存在的目录.

Looks like you're trying to write a file to a directory that doesn't exist.

在调用 np.save()

import os
import numpy as np


# filename for the file you want to save
output_filename = "settings.npy"

homedir = os.path.expanduser("~")

# construct the directory string
pathset = os.path.join(homedir, "\Documents\School Life Diary")

# check the directory does not exist
if not(os.path.exists(pathset)):

    # create the directory you want to save to
    os.mkdir(pathset)

    ds = {"ORE_MAX_GIORNATA": 5}

    # write the file in the new directory
    np.save(os.path.join(pathset, output_filename), ds)

在创建新目录时,如果要创建一个新的目录结构,其深度不止一层,例如创建 level1/level2/level3 (其中不存在这些文件夹),请使用 os.mkdirs 而不是 os.mkdir . os.mkdirs 是递归的,将构造字符串中的所有目录.

When creating your new directory, if you're creating a new directory structure more than one level deep, e.g. creating level1/level2/level3 where none of those folders exist, use os.mkdirs instead of os.mkdir. os.mkdirs is recursive and will construct all of the directories in the string.

这篇关于python-numpy FileNotFoundError:[错误2]没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆