如何用Python获取GoogleDrive中所有文件和文件夹的详细列表?

2026-04-01 23:181阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计780个文字,预计阅读时间需要4分钟。

如何用Python获取GoogleDrive中所有文件和文件夹的详细列表?

使用Python获取Google Drive存储中的文件和文件夹列表,原文链接:https://www.geeksforgeeks.org/get-list-in-Google-drive-storage-use-python/

在本文中,我们将学习如何使用Python获取Google Drive存储中的文件和文件夹列表。

使用Python获取GoogleDrive存储中的文件和文件夹列表 使用 Python 获取 Google Drive 存储中的文件和文件夹列表

原文:www . geesforgeks . org/get-list-in-Google-drive-storage-use-python/

在本文中,我们将了解如何使用 Python 中的 Google Drive API 获取存储在我们的 Google Drive 云存储中的文件(或文件夹)列表。这是一个 REST 应用编程接口,允许您从应用程序或程序中利用谷歌硬盘存储。

因此,让我们创建一个简单的 Python 脚本,与谷歌驱动应用编程接口通信。

要求:

  • Python (2.6 或更高版本)
  • 启用了谷歌驱动的谷歌帐户
  • 谷歌应用编程接口客户端和谷歌 OAuth 库

安装:

通过运行以下命令安装所需的库:

pip install --upgrade google-api-python-client google-auth-www.googleapis.com/auth/drive']# Create a function getFileList with # parameter N which is the length of # the list of files.def getFileList(N):    # Variable creds will store the user access token.    # If no valid token found, we will create one.    creds = None    # The file token.pickle stores the     # user's access and refresh tokens. It is    # created automatically when the authorization     # flow completes for the first time.    # Check if file token.pickle exists    if os.path.exists('token.pickle'):        # Read the token from the file and         # store it in the variable creds        with open('token.pickle', 'rb') as token:            creds = pickle.load(token)    # If no valid credentials are available,     # request the user to log in.    if not creds or not creds.valid:        # If token is expired, it will be refreshed,        # else, we will request a new one.        if creds and creds.expired and creds.refresh_token:            creds.refresh(Request())        else:            flow = InstalledAppFlow.from_client_secrets_file(                'credentials.json', SCOPES)            creds = flow.run_local_server(port=0)        # Save the access token in token.pickle         # file for future usage        with open('token.pickle', 'wb') as token:            pickle.dump(creds, token)    # Connect to the API service    service = build('drive', 'v3', credentials=creds)    # request a list of first N files or     # folders with name and id from the API.    resource = service.files()    result = resource.list(pageSize=N, fields="files(id, name)").execute()    # return the result dictionary containing     # the information about the files    return result# Get list of first 5 files or # folders from our Google Drive Storageresult_dict = getFileList(5)# Extract the list from the dictionaryfile_list = result_dict.get('files')# Print every file's namefor file in file_list:    print(file['name'])

现在,运行脚本:

python3 script.py

这将尝试在默认浏览器中打开一个新窗口。如果失败,请从控制台复制网址,并在浏览器中手动打开它。

现在,如果您还没有登录,请登录您的谷歌帐户。如果有多个帐户,将要求您选择其中一个。然后,单击允许按钮。

如何用Python获取GoogleDrive中所有文件和文件夹的详细列表?

认证完成后,你的浏览器会显示一条消息认证流程已经完成。你可以关上这扇窗户。

一旦身份验证完成,这将打印您的谷歌驱动器存储中第一个 N 文件(或文件夹)的名称。

注意:文件 credentials.json 应该和 Python 脚本在同一个目录下。如果不是这样,您必须在程序中指定文件的完整路径。

本文共计780个文字,预计阅读时间需要4分钟。

如何用Python获取GoogleDrive中所有文件和文件夹的详细列表?

使用Python获取Google Drive存储中的文件和文件夹列表,原文链接:https://www.geeksforgeeks.org/get-list-in-Google-drive-storage-use-python/

在本文中,我们将学习如何使用Python获取Google Drive存储中的文件和文件夹列表。

使用Python获取GoogleDrive存储中的文件和文件夹列表 使用 Python 获取 Google Drive 存储中的文件和文件夹列表

原文:www . geesforgeks . org/get-list-in-Google-drive-storage-use-python/

在本文中,我们将了解如何使用 Python 中的 Google Drive API 获取存储在我们的 Google Drive 云存储中的文件(或文件夹)列表。这是一个 REST 应用编程接口,允许您从应用程序或程序中利用谷歌硬盘存储。

因此,让我们创建一个简单的 Python 脚本,与谷歌驱动应用编程接口通信。

要求:

  • Python (2.6 或更高版本)
  • 启用了谷歌驱动的谷歌帐户
  • 谷歌应用编程接口客户端和谷歌 OAuth 库

安装:

通过运行以下命令安装所需的库:

pip install --upgrade google-api-python-client google-auth-www.googleapis.com/auth/drive']# Create a function getFileList with # parameter N which is the length of # the list of files.def getFileList(N):    # Variable creds will store the user access token.    # If no valid token found, we will create one.    creds = None    # The file token.pickle stores the     # user's access and refresh tokens. It is    # created automatically when the authorization     # flow completes for the first time.    # Check if file token.pickle exists    if os.path.exists('token.pickle'):        # Read the token from the file and         # store it in the variable creds        with open('token.pickle', 'rb') as token:            creds = pickle.load(token)    # If no valid credentials are available,     # request the user to log in.    if not creds or not creds.valid:        # If token is expired, it will be refreshed,        # else, we will request a new one.        if creds and creds.expired and creds.refresh_token:            creds.refresh(Request())        else:            flow = InstalledAppFlow.from_client_secrets_file(                'credentials.json', SCOPES)            creds = flow.run_local_server(port=0)        # Save the access token in token.pickle         # file for future usage        with open('token.pickle', 'wb') as token:            pickle.dump(creds, token)    # Connect to the API service    service = build('drive', 'v3', credentials=creds)    # request a list of first N files or     # folders with name and id from the API.    resource = service.files()    result = resource.list(pageSize=N, fields="files(id, name)").execute()    # return the result dictionary containing     # the information about the files    return result# Get list of first 5 files or # folders from our Google Drive Storageresult_dict = getFileList(5)# Extract the list from the dictionaryfile_list = result_dict.get('files')# Print every file's namefor file in file_list:    print(file['name'])

现在,运行脚本:

python3 script.py

这将尝试在默认浏览器中打开一个新窗口。如果失败,请从控制台复制网址,并在浏览器中手动打开它。

现在,如果您还没有登录,请登录您的谷歌帐户。如果有多个帐户,将要求您选择其中一个。然后,单击允许按钮。

如何用Python获取GoogleDrive中所有文件和文件夹的详细列表?

认证完成后,你的浏览器会显示一条消息认证流程已经完成。你可以关上这扇窗户。

一旦身份验证完成,这将打印您的谷歌驱动器存储中第一个 N 文件(或文件夹)的名称。

注意:文件 credentials.json 应该和 Python 脚本在同一个目录下。如果不是这样,您必须在程序中指定文件的完整路径。