简体   繁体   English

如何使用 python 脚本在谷歌驱动器中创建和显示文件夹?

[英]How to create and show a folder in google drive using python script?

This code is showing the id of the folder_name but it is not showing up the folder_name in google drive.此代码显示了folder_nameid ,但未在 google 驱动器中显示folder_name My purpose is to create and show the folder_name in google drive.我的目的是在谷歌驱动器中创建并显示folder_name名称。 How to do it?怎么做?

import httplib2
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

scope = 'https://www.googleapis.com/auth/drive'

credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
http = httplib2.Http()

drive_service = build('drive', 'v3', http=credentials.authorize(http))

def createFolder(name):
    file_metadata = {
        'name': name,
        'mimeType': 'application/vnd.google-apps.folder'
    }
    file = drive_service.files().create(body=file_metadata,
                                        fields='id').execute()
    print('Folder ID: %s' % file.get('id'))

createFolder('folder_name')

In your script, the folder is created by the service account.在您的脚本中,该文件夹由服务帐户创建。 I think that this is the reason of your issue.我认为这是您的问题的原因。 The Google Drive of service account is different from your Google Drive.服务帐户的 Google Drive 与您的 Google Drive 不同。 By this, the folder created by the service account cannot be seen in your Google Drive using the browser.这样,使用浏览器就无法在您的 Google Drive 中看到由服务帐户创建的文件夹。 When you want to see the folder created by the service account at the browser, how about the following flow?当你想在浏览器上看到服务账户创建的文件夹时,下面的流程怎么样?

  1. Create new folder in your Google Drive of your account.在您帐户的 Google 云端硬盘中创建新文件夹。
    • Please create new folder in your Google Drive.请在您的 Google 云端硬盘中创建新文件夹。 In this case, you can create it using the browser.在这种情况下,您可以使用浏览器创建它。
  2. Share the created new folder with the email of the service account.与服务帐户的 email 共享创建的新文件夹。
    • By this, your created folder in your Google Drive can be accessed by the service account.这样,服务帐户就可以访问您在 Google Drive 中创建的文件夹。
    • The email address can be confirmed in the file of credentials.json . email 地址可以在credentials.json文件中确认。
  3. Create a folder to the shared folder using the service account with the script.使用脚本中的服务帐户为共享文件夹创建一个文件夹。

By this flow, I think that your goal can be achieved.通过这个流程,我认为您的目标可以实现。 For this, please modify your script as follows.为此,请按如下方式修改您的脚本。

From:从:

file_metadata = {
    'name': name,
    'mimeType': 'application/vnd.google-apps.folder'
}

To:至:

file_metadata = {
    'name': name,
    'mimeType': 'application/vnd.google-apps.folder',
    'parents': ['### folder ID ###']
}
  • Please replace ### folder ID ### with your created folder ID in your Google Drive.请将### folder ID ###替换为您在 Google 云端硬盘中创建的文件夹 ID。
  • By above modification, the folder created by the script can be seen at your Google Drive using your browser.通过上述修改,脚本创建的文件夹可以使用浏览器在您的 Google Drive 中看到。

Reference:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在谷歌驱动器上创建一个文件夹(如果不存在)并使用 Python 脚本将文件上传到它 - Create a folder (if not exists) on google drive and upload a file to it using Python script 我可以使用 Python 服务帐户创建 Google Drive 文件夹吗? - Can I create a Google Drive folder using service account with Python? Python-使用gspread在Google云端硬盘的特定文件夹中创建电子表格 - Python - Using gspread to create a spreadsheet in a specific folder in Google Drive 如何使用 python 在谷歌驱动器中创建目录? - How to create directories in google drive using python? 如何在 Python 中使用 Google Drive API 创建新文件夹? - How can I create a new folder with Google Drive API in Python? 如何使用python和Google云端硬盘上传到文件夹并替换文件? - how to upload to folder and replace files using python and Google Drive? Python:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹 - Python: How to upload files to a specific folder in Google Drive using folderId 如何在 python 中使用 pydrive 在谷歌驱动器中查找子文件夹 ID - How to find the sub folder id in google drive using pydrive in python 如何使用 Google 的 Python Drive Client API 在 Team Drive 中创建文件夹? - How to create a folder in Team Drive with Google's Python Drive Client API? 如何使用Python脚本将文件上传到Google云端硬盘? - How to upload a file to Google Drive using a Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM