Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

File not closing after Upload() #129

Open
robrothschild opened this issue May 1, 2018 · 6 comments
Open

File not closing after Upload() #129

robrothschild opened this issue May 1, 2018 · 6 comments

Comments

@robrothschild
Copy link

I'm trying to delete a .zip file from my local system after uploading it to drive. The issue is that one the file is uploaded I get the following error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'test.zip'

To delete the file I use os.remove but even when I try to remove it from the file manager it wont let me.

@keliomer
Copy link

keliomer commented May 9, 2018

Can you post the code you use to upload?

@robrothschild
Copy link
Author

robrothschild commented May 9, 2018

Sure thing, this is in a for loop that uploads a .png file, then deletes it. It fails on the first run through.

upfile = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": out_folder}]})
upfile.SetContentFile(plot)
upfile.Upload()
os.remove(plot)

It gets that WinError32 on os.remove()

@bonny1992
Copy link

bonny1992 commented Sep 13, 2018

Can confirm.
I can't edit any file after the Upload() method.

Here's my code:

fileHandle.SetContentFile(compressed_file_path)
fileHandle['title'] = new_filename
fileHandle['mimeType'] = 'application/gzip'
fileHandle.Upload()
os.remove(compressed_file_path)

And here's the traceback (with shortened path)
PermissionError: [WinError 32] Impossibile accedere al file. Il file è utilizzato da un altro processo: 'C:\\test\test.tar.gz'

Any update on this?

EDIT:
I created a pull request (sorry if malformed or anything, I'm not familiar at all) with what I think is a fix to this.
#144

@bjgau
Copy link

bjgau commented Mar 30, 2019

I found a work around where you can basically just free the variable using something like del fileHandle and then use the os.remove(compressed_file_path) to delete the file.

@csimmons0
Copy link

I found a solid workaround that uses PyDrive's public API:
SetContentFile("nul")
"nul" is the Windows equivalent to /dev/null.

I think the ideal fix is to make SetContentFile not open the file as it does here: https://github.com/gsuitedevs/PyDrive/blob/68cea204cdcd10bab9321580164c8f4961385a0f/pydrive/files.py#L175
But instead open the file immediately before and after the upload.

@zjcnew
Copy link

zjcnew commented Apr 26, 2021

Sure thing, this is in a for loop that uploads a .png file, then deletes it. It fails on the first run through.

upfile = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": out_folder}]})
upfile.SetContentFile(plot)
upfile.Upload()
os.remove(plot)

It gets that WinError32 on os.remove()

@robrothschild You need to manually close the file handle

upfile = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": out_folder}]})
upfile.SetContentFile(plot)
upfile.Upload()
upfile.content.close()
os.remove(plot)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants