0

I am trying to read/get data from a json file. This json file is stored in the project > Requests > request1.json. In a script i am trying to read data from the json file and failing badly. This is the code i'm trying to use to open file in read mode.

Trying to replace(in windows)

  f = open('D:\\Test\\projectname\\RequestJson\\request1.json', 'r') with 

  f = open(os.path.expanduser('~user') + "Requests/request1.json", 'r')

Any help would be greatly appreciated.

user29496
  • 65
  • 8
  • Does this answer your question? [How to get the home directory in Python?](https://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python) – Max Carroll Jun 17 '20 at 00:06
  • 1
    what exactly do you mean with `user`? Recall that the default disk for user in windows is `C` yet your file is stored in `D` – Onyambu Jun 17 '20 at 00:06

1 Answers1

1

Using current directory path (assuming that is in the project) and appending the remaining static file path:

import os
current_dir = os.path.abspath(os.getcwd())

path = current_dir + "/RequestJson/request1.json"

with open(path, 'r') as f:
    f.write(data)
DirtyBit
  • 15,671
  • 4
  • 26
  • 53