2

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.

RMPR
  • 3,368
  • 4
  • 19
  • 31
ankur singh
  • 72
  • 1
  • 7

1 Answers1

2
  • You want to upload a file of C:\\Users\\New\\Pictures\\Screenshots\\older\\Screenshot.png' to Google Drive using pydrive with python.
  • You have already been able to use Drive API.

If my understanding is correct, how about this modification?

Modification point:

  • In your script, the file content is not given.

Modified script:

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()

References:

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

Tanaike
  • 181,128
  • 11
  • 97
  • 165