How to open JSON file from another directory - json

I am trying to open JSON file which is located in another directory, and receiving error:
FileNotFoundError: [Errno 2] No such file or directory:
I understand that if I provide relative path, the file has to be in same directory, otherwise the full (root) path has to be provided.
My question is how to avoid it, as at the moment I test it locally, but the code is being used by other people so obviously the path can't be from my root.
Any idea of how to resolve it?
Here is the code:
with open("example.json") as commands:
commands = json.load(commands)

You can specify a path relative to your current location.
For example, you're in folder baz and the json file is in folder foo
my
├── bar
│   └── baz <--- you're here
└── foo
└── example.json <--- the file is here
you can access the json file with
with open("../../foo/example.json") as commands:
commands = json.load(commands)
where .. is the parent of a folder. So ../../foo/example.json is the file two parents up (baz -> bar -> my) and then into the folder foo and finally to the json file example.json.
 
Finally note that if you are on Windows you might need to replace the forward slashes (/) in the path with backward slashes (\).

Related

Weird warning in gcloud console: The path is in a mounted secrets volume [...] but does not correspond to any secret

I'm deploying a gcloud function with two mounted secrets (from google secret manager), my local dir structure is the following:
├── index.js
├── mounted-secret-config
│ ├── config.js
├── mounted-secret-credentials
│ └── googleServiceAccountCredentials.json
├── package-lock.json
└── package.json
config.js and googleServiceAccountCredentials.json are ignored so the deploy process doesn't upload them.
I deploy using this command:
gcloud functions deploy <...> --region <...> --trigger-http --runtime nodejs16 --allow-unauthenticated --gen2 --memory 256Mi --set-secrets=/workspace/mounted-secret-config/config.js=configjs:latest,/workspace/mounted-secret-credentials/googleServiceAccountCredentials.json=googleServiceAccountCredentials:latest
It works, the node app finds the files and overall works but after each deploy I see this in the gcloud logs:
2022-08-26 10:11:18.130 CEST Could not open file at path /secret_volume_0/config. The path is in a mounted secrets volume, but the exact path does not correspond to any secret specified in the mount configuration.
Warning
2022-08-26 10:11:18.182 CEST Could not open file at path /secret_volume_0/package.json. The path is in a mounted secrets volume, but the exact path does not correspond to any secret specified in the mount configuration.
Warning
And after each http request to my service i get:
2022-08-26 10:05:33.511 CEST Could not open file at path /workspace/mounted-secret-config/config. The path is in a mounted secrets volume, but the exact path does not correspond to any secret specified in the mount configuration.
Warning
2022-08-26 10:05:33.572 CEST Could not open file at path /workspace/mounted-secret-config/package.json. The path is in a mounted secrets volume, but the exact path does not correspond to any secret specified in the mount configuration.
I've no idea what's going on here, I don't even know who's logging this. /workspace/mounted-secret-config/config doesn't exist, but /workspace/mounted-secret-config/config.js (note the .js extension) does, and the app finds it or it would not even start. /workspace/mounted-secret-config/package.json this doesn't but it isn't supposed to, who's even trying to access it? And why it doesn't complain about the other mounted secret?
config.js is required with: require('./mounted-secret-config/config')
If I change it to require('./mounted-secret-config/config.js') (adding .js) one of the two warnings disappears. Is node trying to import the exact name (giving the warning) and then falling back to config.js? But what about the package.json?
This is not a real solution, more like a workaround, feel free to add a real solution if you find it.
What I did to "fix" the problem was change the format of my config file from .js to .json (easy in my case because it only contained a dictionary with few keys/values)
Everything else stayed the same, I still read the file with require. I don't know what weird thing node was doing with the .js, but it doesn't do it with .json.
Both warning disappeared (including the one about package.json).

No file or variants found for asset: assets/credentials.json error when trying to load in JSON credentials

I am trying to pass in some json credentials to my app but get the below error:
Error detected in pubspec.yaml:
No file or variants found for asset: assets/credentials.json.
Any ideas? In there are credentials in the credentials.json file, so I am unsure why that error is being thrown.
Step 1: make sure assets folder is in the same level with lib folder, not under lib forlder
Step 2: make sure pubspec.yaml file has correct indent
Step 3: make sure you have put file credentials.json under assets folder
Make sure on pubspec.yaml than all your assets definitions for folders ends with /.
Example:
assets:
- assets/

GC Cloud function Error from file zip: File main.py that is expected to define function doesn't exist

Setting up a cloud function via zip file with python files. Project is set up as
. project_cf
|
main.py
requirements.txt
otherlib.py
more.py
When I go to upload the project I receive the following message: Function failed on loading user code. Error message: File main.py that is expected to define function doesn't exist
main.py is called via main:start.
This can be resolved by ensuring when zipping the folder you don't actually zip the folder, just the files.
See the note in deployment documentation:
Note: Make sure your source files are at the root of the ZIP file,
rather than a folder containing the files.
You can do this via -
cd your_project_directory
zip -r your_project.zip . (osx)
This puts all the files into one zip at the root, rather than in a directory down.

java.io.FileNotFoundException in Jmeter even if file is in bin folder itself

java.io.FileNotFoundException in Jmeter even if file is in bin folder itself
I have created a file in jmeter/bin Folder and then used it in Jmeter script without any path (only file name as it is already in bin folder)
Still I am getting error as java.io.FileNotFoundException
Filename path could be relative (to your .jmx file) or absolute (/home/username...).
Check your path for trailing spaces - jmeter doesnt know how to trim it.
Actually relative path in CSV Data Set Config (not in distributed test) is relative to test plan and not jmeter execution directory:
Filename Name of the file to be read. Relative file names are resolved with respect to the path of the active test plan.
You can also use absolute path or move your CSV file to same directory of your JMX files.

read json in python3 from relative path

This is my project structure:
main
-/data/data.json
-/a/b/c.py
main.py
data folder an a folder are in main folder which is the root of my project.
I need to read data.json from c.py in python3
what would be my string in open()?
file = open('./data/data.json')
this works in windows but not linux
Up two folders and then in data/data.json:
with open('../../data/data.json') as f:
...
If you are on Windows, swap slashes with backslashes.