我是Google drive API的新手,并且编写了一个最简单的脚本形式,可以自动将图像从本地驱动器上传到google驱动器,然后一旦该图像上传,删除本地副本,以下是我得到的:
#%%
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from googleapiclient.http import MediaFileUpload
g_login = GoogleAuth()
g_login.LocalWebserverAuth()
drive = GoogleDrive(g_login)
#%%
header = 'images/dice'
path = header + str(i) + '.png'
file = drive.CreateFile()
file.SetContentFile(path)
file.Upload()
if file.uploaded:
print("test")
os.remove(path)但是,在尝试删除本地副本时,出现以下错误:
PermissionError: WinError 32该进程无法访问该文件,因为该文件正被另一个进程使用:‘image/dice1.png’
我对它进行了搜索,认为可能是SetContentFile(路径),在上载()之后没有关闭文件,根据
https://gsuitedevs.github.io/PyDrive/docs/build/html/pydrive.html
它应该会在上传后自动关闭。
我在这里监督什么?
注意:最后,我想使用一个循环来遍历目录中的所有文件。
这是输出:
1
test
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
<ipython-input-21-2aeb578b5851> in <module>
9 if file.uploaded:
10 print("test")
---> 11 os.remove(path)
12
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'images/dice1.png'

发布于 2020-02-12 15:53:39
即使PyDrive不会为您关闭它,但通过查看代码,您看起来可以这样做:
...
try:
file.Upload()
finally:
file.content.close()
if file.uploaded:
...你能试一试吗?看看有没有帮助?
https://stackoverflow.com/questions/60132814
复制相似问题