google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

Issue with file closing in MediaFileUpload

Open IAMVanilka opened this issue 1 year ago • 0 comments

When using the MediaFileUpload class to upload files to Google Drive via google-api-python-client, the file remains open after the upload completes, resulting in a PermissionError when I attempt to delete the file.

Code example:

from googleapiclient.http import MediaFileUpload

file_metadata = {'name': "file.zip"}
media = MediaFileUpload('D:\\file.zip', mimetype='application/zip', resumable=True)
request = drive.files().create(body=file_metadata, media_body=media)
response = request.execute()

os.remove('D:\\file.zip')  # Raises PermissionError

Expected behavior: The file should automatically close after uploading so that it can be deleted without errors.

Actual behavior: The file remains open, and attempting to delete it results in a PermissionError.

Solution: I resolved the issue manually by adding media._fd.close() after the upload completes to close the file.

IAMVanilka avatar Sep 20 '24 04:09 IAMVanilka