16

I am trying to upload a file to my Google drive, the code below works. How can I specify to which folder to upload to i.e drive---shared with me--csvFolder

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


gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()
Tanaike
  • 181,128
  • 11
  • 97
  • 165
programmerwiz32
  • 529
  • 1
  • 5
  • 20

1 Answers1

25
  • You want to upload a file to the specific folder in your Google Drive using pydrive.

If my understanding is correct, how about this modification?

From:

file2 = drive.CreateFile()

To:

file2 = drive.CreateFile({'parents': [{'id': '### folder ID ###'}]})
  • Please set the folder ID like above.

Reference:

If this was not the result you want, I apologize.

Added:

When you want to upload a file to the specific folder from the folder name, how about this modification?

From:

file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()

To:

folderName = '###'  # Please set the folder name.

folders = drive.ListFile(
    {'q': "title='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
for folder in folders:
    if folder['title'] == folderName:
        file2 = drive.CreateFile({'parents': [{'id': folder['id']}]})
        file2.SetContentFile('new_test.csv')
        file2.Upload()

Alternative to get folder ID

You can use the following snippet to print files and or folders ID

fileList = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in fileList:
  print('Title: %s, ID: %s' % (file['title'], file['id']))
  # Get the folder ID that you want
  if(file['title'] == "To Share"):
      fileID = file['id']
Areza
  • 5,623
  • 7
  • 48
  • 79
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • how do I get the id for the folder? – programmerwiz32 Jun 03 '19 at 21:56
  • @programmerwiz32 I proposed one more modification. In this modification, the file can be uploaded to the specific folder using the folder name. Could you please confirm it? – Tanaike Jun 03 '19 at 22:13
  • thanks it works,Is there a way to stop google from poping up the login page when I run code (my account is logged in already) i.e choosing google account and then allow access? – programmerwiz32 Jun 03 '19 at 22:51
  • @programmerwiz32 Thank you for replying. I'm glad your issue was resolved. About your new issue, unfortunately, I couldn't replicate your situation. I apologize for this situation. So can you post it by including more information as new question? By this, users including me can think of your situation. I would like to confirm your situation. If you can cooperate to resolve your issue, I'm glad. – Tanaike Jun 03 '19 at 22:55
  • @programmerwiz32 Thank you for your response. By the way, what is the tag? – Tanaike Jun 03 '19 at 23:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194399/discussion-between-programmerwiz32-and-tanaike). – programmerwiz32 Jun 03 '19 at 23:01