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()
Google offers Backup and Sync, an application you can install on your computer in order to back up any folder on your computer over to Google Drive automatically. Simply install Backup and Sync and you can add any folder on your computer to automatically upload all files to Google Drive.
Method 1: Using the Python's os Module: Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want. Lastly, we need the input tag with the filename attribute to upload the file we want.
If my understanding is correct, how about this modification?
file2 = drive.CreateFile()
file2 = drive.CreateFile({'parents': [{'id': '### folder ID ###'}]})
If this was not the result you want, I apologize.
When you want to upload a file to the specific folder from the folder name, how about this modification?
file2 = drive.CreateFile()
file2.SetContentFile('new_test.csv')
file2.Upload()
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()
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With