I am trying to upload a file from my computer to google drive using Python(Pydrive), it gets uploaded succesfully but gets damaged and its size becomes 0kb.
Here is my code:
drive = GoogleDrive(gauth)
import glob, os
with open('C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png', 'r') as file:
fn = os.path.basename(file.name)
file_drive = drive.CreateFile({"title": fn })
file_drive.Upload()
print('The file: ' + fn + ' has been uploaded')
print('Successful')
What I am doing wrong? I have done the Authentication part and it's Authenticated successfully.
I have checked answers related to this but I didn't get the answer I am looking for.
C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png'
to Google Drive using pydrive with python.If my understanding is correct, how about this modification?
Please modify your script as follows.
From:with open('C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png', 'r') as file:
fn = os.path.basename(file.name)
file_drive = drive.CreateFile({"title": fn })
file_drive.Upload()
To:
f = 'C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png' # Added
with open(f, 'r') as file: # Modified
fn = os.path.basename(file.name)
file_drive = drive.CreateFile({"title": fn })
file_drive.SetContentFile(f) # Added
file_drive.Upload()
If this was not the direction you want, I apologize.