Last active
November 1, 2020 15:43
-
-
Save bhavsarpratik/0be9e49b210b5dac749f06ea2e719cf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# !pip install tqdm pydrive joblib" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from pydrive.auth import GoogleAuth\n", | |
| "from pydrive.drive import GoogleDrive\n", | |
| "from google.colab import auth\n", | |
| "from oauth2client.client import GoogleCredentials\n", | |
| "\n", | |
| "from tqdm import tqdm\n", | |
| "\n", | |
| "#For local usage\n", | |
| "gauth = GoogleAuth()\n", | |
| "gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script\n", | |
| "drive = GoogleDrive(gauth)\n", | |
| "\n", | |
| "#For google colab usage\n", | |
| "# auth.authenticate_user()\n", | |
| "# gauth = GoogleAuth()\n", | |
| "# gauth.credentials = GoogleCredentials.get_application_default()\n", | |
| "# drive = GoogleDrive(gauth)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Get files by filter logic" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# https://developers.google.com/drive/api/v2/search-files#python\n", | |
| "fileList = drive.ListFile({'q': \"title contains '.pkl' and trashed=false\"}).GetList()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "print(len(fileList))\n", | |
| "fileList[0]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Unparallel" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "for file in tqdm(fileList):\n", | |
| " file.Delete() # Permanently delete the file." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Parallel" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from joblib import Parallel, delayed\n", | |
| "\n", | |
| "def delete_file(file):\n", | |
| " file.Delete() \n", | |
| "\n", | |
| "Parallel(n_jobs=4)(delayed(delete_file)(i) for i in fileList)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.7.0" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 4 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment