Save the Pydrive authentication file in a different directory from the script

Conclusion

If you want to save the Pydrive authentication file in a different directory from the script, write as follows.

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.file import Storage

gauth = GoogleAuth(settings_file=f"{Absolute Path of the directory you want to save}/settings.yaml")
gauth.credentials = Storage(f"{Absolute Path of the directory you want to save}/credentials.json").get()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)

Dependent environment

python==3.7.6
pydrive==1.3.1

background

Introduction

The basic usage and setting method of Pydrive are omitted because the following sites carefully explain it.

Reference site: Download, upload, delete Google Drive with Python, PyDrive, etc .-- note.nkmk.me

By default, the authentication file cannot be saved in another directory.

If the Pydrive authentication file is not saved in the same directory where the script is saved (by default), the authentication will be NG.

For example, it is a must to save the authentication files client_secrets.json, credentials.json, settings.yaml in the same directory as the script main.py as shown below.

/home/hogehoge/
  └ hoge_project/
    └ src/
      ├ client_secrets.json
      ├ credentials.json
      ├ settings.yaml
      └ main.py

main.py


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)

But I want to save it in another directory.

However, it feels awkward to have scripts and authentication files mixed in the src directory. So somehow, I want to move the authentication file under the creds / directory as follows.

/home/hogehoge/
  └ hoge_project/
    ├ creds/
    │  ├ client_secrets.json
    │  ├ credentials.json
    │  └ settings.yaml
    │
    └ src/
      └ main.py

By specifying the argument, it is possible to read the authentication file in another directory.

If you write as follows, main.py can recognize the authentication file undercredits /.

main.py


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.file import Storage

gauth = GoogleAuth(settings_file=f"/home/hogehoge/creds/settings.yaml")
gauth.credentials = Storage(f"/home/hogehoge/creds/credentials.json").get()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)

Reference site: [Note that you stumbled on scraping python --Qiita](https://qiita.com/nacopon/items/515194278d0aa875cca0#pydrive-crontab%E3%81%A7%E5%AE%9F%E8%A1 % 8C% E3% 81% 99% E3% 82% 8B% E3% 81% A8% E3% 82% A8% E3% 83% A9% E3% 83% BC)

at the end

I had a hard time finding information. Please let me know if there is anything else.

Recommended Posts

Save the Pydrive authentication file in a different directory from the script
How to read a file in a different directory
Save the pystan model and results in a pickle file
Access the file with a relative path from the execution script.
[Note] Import of a file in the parent directory in Python
Save the binary file in Python
Save a YAML-formatted file in PyYAML
Process the contents of the file in order with a shell script
Read a file in Python with a relative path from the program
[Note] Read a file from another directory
Script to generate directory from json file
Run the Python interpreter in a script
Replace the directory name and the file name in the directory together with a Linux command.
View the full path (absolute path) of a file in a directory in Linux Bash
Extract only the file name excluding the directory in the directory
Save the object to a file with pickle
Call a Python script from Embedded Python in C ++ / C ++
Access the variables defined in the script from the REPL
Save the setting conditions as a CSV file using UDF Manager in OCTA
A python script that draws a band diagram from the VASP output file EIGENVAL
Get the file name in a folder using glob
A memorandum to run a python script in a bat file
Python script to create a JSON file from a CSV file
Create a script for your Pepper skill in a spreadsheet and load SayText directly from the script
[Sublime Text 2] Always execute a specific file in the project
Change the standard output destination to a file in Python
Different from the import type of python. from A import B meaning
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
Process the files in the folder in order with a shell script
I wrote a script that splits the image in two
Download the file in Python
File access under the directory
Settings for running a test each time you save a file in the editor using watchmedo (watchdog)
Output the key list included in S3 Bucket to a file
Search the file name including the specified word and extension in the directory
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
Anyway, the fastest serial communication log is left in a file
Outputs a line containing the specified character string from a text file
Read the config file in Go language! Introducing a simple sample
Dig the directory and create a list of directory paths + file names
Get the formula in an excel file as a string in Python
Get OCTA simulation conditions from a file and save with pandas
Run a multi-line script in a PDB
Execute Python script from batch file
Get the script path in Python
Create a binary file in Python
The story of the "hole" in the file
A memo organized by renaming the file names in the folder with python
How to specify a .ui file in the dialog / widget GUI in PySide
Extract lines that match the conditions from a text file with python
When you make a mistake in the directory where you execute `pipenv install`
Test & Debug Tips: Create a file of the specified size in Python
I made a program to check the size of a file in Python
I created a script to check if English is entered in the specified position of the JSON file in Python.
How to save the feature point information of an image in a file and use it for matching
[Unzip] How to decompress only arbitrary files from those whose directory delimiter in the zip file is backslash