4

I have this code:

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)

But the error that he gave me is:

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...

Thanks

maicol07
  • 199
  • 1
  • 6
  • 16

1 Answers1

6

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

Try using os.mkdir to create the directory to save into before calling 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)

EDIT:

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.

RHSmith159
  • 1,823
  • 9
  • 16
  • Thanks, but I have one question: Why I can't see the folder created? – maicol07 Aug 16 '17 at 17:02
  • Also, if I install it on a computer without Python, with a cx_freeze MSI installer, it gave me WinError 3 – maicol07 Aug 16 '17 at 18:45
  • 1
    I'm not entirely sure what you mean by 'see the folder', do you mean in Explorer? also, with regards to the `WinError 3`, if you're compiling the python program into a .exe for a windows machine, I'm pretty sure any python based .exe will need python installed on the machine, I could be wrong about that though – RHSmith159 Aug 17 '17 at 07:16
  • Yes, I mean in Explorer. So any PC with the app installed by a .msi installer built with cx_freeze needs Python to have no error? – maicol07 Aug 17 '17 at 10:03
  • Its been a while since I've made a python exe for windows, but I believe so. With regards to your missing folder, are you sure its creating the directory where you expect it to? If you use windows explorer to do a search for the folder, can you find it then? – RHSmith159 Aug 17 '17 at 10:09
  • Something doesn't work... I found the folder but it's not where I want... It's in C: so the variable pathset is `C:\Documents\School Life Diary` but I want `C:\Users\nameoftheuser\Documents\School Life Diary"`. To do this I imported the module `getpass` and changed homedir to `"~"+getpass.getuser()`. The homedir is correct but the pathset no... Have you got any idea? Also I'd like to ask you if users that install my app through installer need also numpy installed. Thanks – maicol07 Aug 22 '17 at 10:35
  • 2
    Nevermind, solved it: `os.path.expanduser(r'~\Documents\School Life Diary')` – maicol07 Aug 22 '17 at 18:12
  • 1
    ah neat fix! also, its been a while since I've made python exe files for windows, but I think there are ways of compiling to exe such that the users wont need to install python or the used modules, this answer will be a good place to start for you: `https://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency` – RHSmith159 Aug 23 '17 at 07:25