Home > Enterprise >  File not found error on creating a new file and writing in it
File not found error on creating a new file and writing in it

Time:03-26

I am trying to create a new file and write on it . but when I am doing something like this I am getting file not found error. seems I am missing something which I can't figure out .

data = {
    "table_name": "name",
}
file_path = '/tmp/myfile.json'
with open(file_path   '.json', 'w') as f:
    json.dump(data, f, indent=2)

[ERROR] FileNotFoundError: [Errno 2] No such file or directory: '/tmp/myfile.json'

CodePudding user response:

are you trying to run this on Windows by any chance? Or a *nix system where you don't have access to /tmp?

For me, this script runs fine on a Ubuntu and MacOS (although the created file is /tmp/myfile.json.json instead of /tmp/myfile.json because of the '.json' in your script), but not on Windows.

CodePudding user response:

well python tries to find the file in the cuurent working directory, check if the cwd is what you think it is

import pathlib

print(pathlib.Path().absolute())

u can do this to check the cwd

  • Related