LearningSlowly LearningSlowly - 1 year ago 130
JSON Question

Python OS symlink and .json.gz files

I am creating a symlink with Python. First, I check if the file exists and then create the symlink.

import os
project = 'test'

if os.path.isfile("../../project/json/nodes1.json.gz"):

However, when I ran a script that required this symlink, it failed. On inspection with finder I get
The operation can’t be completed because the original item for “nodes1.json.gz” can’t be found.

I am sure I have the right paths and it is correctly setting the symlink. Is there an issue with using

Answer Source

symlink paths are relative to the directory where the symlink lives. Your real file is two levels up from the current directory but your symlink is only one level up and then an extra level deep. If I make up names for your parent directories, it would be something like

current directory     aaa/bbb/ccc
real file             aaa/project/json/nodes1.json.gz
symlink               aaa/bbb/simulations/project/nodes1.json.gz

Your symlink should be one more level deep


A good way to check symlinks is to go into the directory holding the link and then follow it to see where things go wrong.

td@mintyfresh ~/tmp/deleteme $ tree
└── aaa
    ├── bbb
    │   ├── ccc
    │   └── simulations
    │       └── project
    │           └── nodes1.json.gz -> ../../project/json/nodes1.json.gz
    └── project
        └── json
            └── nodes1.json.gz

7 directories, 2 files
td@mintyfresh ~/tmp/deleteme $ cd aaa/bbb/simulations/project
td@mintyfresh ~/tmp/deleteme/aaa/bbb/simulations/project $ ls -l
total 0
lrwxrwxrwx 1 td td 36 Jun 16 12:06 nodes1.json.gz -> ../../project/json/nodes1.json.gz
td@mintyfresh ~/tmp/deleteme/aaa/bbb/simulations/project $ cd ..
td@mintyfresh ~/tmp/deleteme/aaa/bbb/simulations $ cd ..
td@mintyfresh ~/tmp/deleteme/aaa/bbb $ cd project
bash: cd: project: No such file or directory