簡體   English   中英

如何在 yaml 文件中提及 Mac 主文件夾

[英]How to mention Mac Home folder in yaml file

有人能告訴我如何在 yaml 文件中提及 Mac 主文件夾嗎?

我有一個 settings.yaml 文件,我在 Python 代碼中使用它來連接到 GDrive。 它將憑據導出到 JSON 文件,我希望將其提取到主目錄中的 GDrive 文件夾。

設置.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'

在 python 文件中,它是這樣提到的:

gauth = GoogleAuth(settings_file='settings.yaml')
gauth.LoadCredentialsFile('/Users/machinename/GDrive/credentials.json')

所以,不同的人會在不同的機器上運行它,機器machinename會有所不同,所以我試圖為主目錄提供動態版本。 我試過~/GDrive/credentials.json但沒有用。

有人可以幫助如何在 yaml 文件中聲明主目錄嗎?

主文件夾表示為~
問題是您需要擴展,您可以使用os來完成:

import os

os.path.expanduser("~/GDrive/credentials.json")

更新

如果要存放在 yaml 代碼中,也是一樣的:

設置.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'

然后,在 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'))

這可能會達到你想要的

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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM