3

I'm using PyDrive to upload files to Google Drive but could not figure out how to define the destination folder.

The following code saves a copy to the root on GDrive:

gauth = GoogleAuth() 
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({'parent': '/home/pi'}) 
file1.SetContentFile('test.txt')
file1.Upload()

And how PyDrive returns the upload/download result?

5

First you need to obtain the id of the parent folder (the one you want to place files into). Use this code to do so:

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:       
    print ('title: %s, id: %s' % (file1['title'], file1['id']))
sys.exit()

save the folder id in a variable:

fid = '...'

Then, to add to the folder corresponding to that id, the client code is:

f = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": fid}]})
f.SetContentFile( some_path )
f.Upload()

You can get some info about the result:

print 'Created file %s with mimeType %s' % (f['title'], f['mimeType'])        
  • This answer shows how to upload a file to a specific folder, which is what I was looking for. Thanks – FindOutIslamNow Feb 3 at 11:36
  • You're welcome. – g33kz0r Feb 7 at 1:47
4

This works:

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

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

f = drive.CreateFile()
f.SetContentFile('document.txt')
f.Upload()
print('title: %s, mimeType: %s' % (f['title'], f['mimeType']))

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.