7

I want to create a file outside the current working directory in python. Here's my directory structure.

|--myproject
|   |-- gui
|   |   |-- modules
|   |   |   |-- energy
|   |   |   |   |-- configuration
|   |   |   |   |   |-- working_file.py
|   |-- service
|   |   |-- constants
|   |   |   |-- global_variables.json    

I'm current working in /myproject/gui/energy/configuration/working_file.py and I want to create a file under /myproject/service/constants named global_variables.json

I tried

with open("../../../../../service/constants/global_variables.json", 'w') as file_handler:
        content = json.load(file_handler)
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

5 Answers5

6

Relative paths are resolved from the current working directory and not from the directory where the script is located. If the file you're trying to create needs to be on a specific directory, use an absolute path (e.g. /absolute/path/to/myproject/service/constants/global_variables.json).

If you can't know this absolute path, refer to this SO question

Community
  • 1
  • 1
svvac
  • 5,814
  • 3
  • 17
  • 22
3

Python doesn't interpret ../, it will look for a directory named ".." in the cwd.

You either have to hardcode the path:

with open("/path/to/myproject/service/constants/global_variables.json", 'w') as file_handler:
    content = json.load(file_handler)

Or find the full path to the current executing script:

EDIT: I was wrong, python does interpret "..", what's happening here is that's is start is the cwd not your script.

$ echo 'Hello world' > text_file.txt
$ mkdir test/
$ cd test
$ python
[...]
>>> open('../text_file.txt').read()
'Hello world\n'
Community
  • 1
  • 1
ThinkChaos
  • 1,803
  • 2
  • 13
  • 21
0

Are you sure the path is correct?

Assuming that the current path is: ../myproject/gui/modules/energy/configuration

The path that you mention is:

"..(a)/..(b)/..(c)/..(d)/..(e)/service/constants/global_variables.json"  

(a) = energy/
(b) = modules/
(c) = gui/
(d) = myproject/
(e) = ../

your services directory is I think in the myproject directory not in the one before it. Not sure if that is your question ... Is that your question?

ssm
  • 5,277
  • 1
  • 24
  • 42
0

What you can do is find current file path and script directory path from that path, like this

dir = os.path.dirname(__file__)

And then you can add or join the place where you want to create file respective to this path

jsonfilepath = "../../../../../service/constants/global_variables.json"

reljsonfilepath = os.path.join(dir, jsonfilepath)

f = open (reljsonfilepath, 'w')

Please check as this is untested code.

0

For the given below Project Structure:

.
`-- myproject
    |-- gui
    |   `-- energy
    |       `-- configuration
    |           `-- test.py
    `-- services
        `-- constants
            `-- out.txt


import os

## Finding absolute path of the current module
drive, tcase_dir = os.path.splitdrive(os.path.abspath(__file__))

## It's good if we always traverse from the project root directory
## rather than the relative path
## So finding the Project's root directory
paths = tcase_dir.split(os.sep)[:-4]
base_dir = os.path.join(drive,os.sep,*paths)


## Known Sub-Directories
SERVICES_DIR = r'services'
CONSTANTS_DIR = r'constants'

## absolute path to the ../myproject/service/constants/ directory
constants_abs_path = os.path.join(base_dir, SERVICES_DIR, CONSTANTS_DIR)

with open(os.path.join(constants_abs_path, r'out.txt'), 'r') as fp:
    ## Do the file Operations here ##
Siva Cn
  • 929
  • 4
  • 10