Can someone tell me how to mention Mac Home folder in yaml file?
I have a settings.yaml file that I use in my Python code to connect to GDrive. It exports the credentials to a JSON file and I want that to extract to GDrive folder in Home directory.
settings.yaml
client_config_backend: 'settings'
client_config:
client_id: "something"
client_secret: "something"
auth_uri: "https://accounts.google.com/o/oauth2/auth"
token_uri: "https://oauth2.googleapis.com/token"
save_credentials: True
save_credentials_backend: 'file'
save_credentials_file: '/Users/machinename/GDrive/credentials.json'
And in the python file, it is mentioned like this:
gauth = GoogleAuth(settings_file='settings.yaml')
gauth.LoadCredentialsFile('/Users/machinename/GDrive/credentials.json')
So, different people will run this in different machines and the machinename
will vary, so I am trying to provide the dynamic version for Home directory. I tried ~/GDrive/credentials.json
but it did not work.
Can someone help how to declare home directory in the yaml file?
The home folder is denoted as ~
.
The problem is you need to expand, and you can do by using os
:
import os
os.path.expanduser("~/GDrive/credentials.json")
UPDATE
If you want to store it in the yaml code, it's the same thing:
settings.yaml
client_config_backend: 'settings'
client_config:
client_id: "something"
client_secret: "something"
auth_uri: "https://accounts.google.com/o/oauth2/auth"
token_uri: "https://oauth2.googleapis.com/token"
save_credentials: True
save_credentials_backend: 'file'
save_credentials_file: '~/GDrive/credentials.json'
Then, in the python:
credentials_file = "settings.yaml"
with open(credentials_file, "r") as f:
credentials_info = yaml.load(f)
gauth = GoogleAuth(settings_file=credentials_file)
gauth.LoadCredentialsFile(os.path.expanduser(credentials_info['save_credentials_file'))
This may achieve what you wanted
In settings.yaml
save_credentials_file: '$HOME/GDrive/credentials.json'
...
In python, put '$HOME/GDrive/credentials.json' into variable save_credentials_file,
gauth.LoadCredentialsFile(os.path.expandvars(save_credentials_file))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.