Skip to content

Issue with file closing in MediaFileUpload #2489

Open
@IAMVanilka

Description

@IAMVanilka

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions