Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with file closing in MediaFileUpload #2489

Open
IAMVanilka opened this issue Sep 20, 2024 · 0 comments
Open

Issue with file closing in MediaFileUpload #2489

IAMVanilka opened this issue Sep 20, 2024 · 0 comments
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@IAMVanilka
Copy link

IAMVanilka commented Sep 20, 2024

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.

@vchudnov-g vchudnov-g removed their assignment Sep 23, 2024
@vchudnov-g vchudnov-g added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. labels Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants