我正在尝试使用pydrive模块自动从谷歌驱动器下载一个简单的文本文件。我一直收到以下错误:
追踪(最近一次通话): 文件" C:\ GIS \ AVGOPS \ Scripts \ GoogleDrive_Test.py",第20行,在 item.GetContentFile(r' C:\ Users \ pfilyer \ Desktop \ googedrive \' + item [' title']) GetContentFile中的文件" C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ pydrive \ files.py",第210行 self.FetchContent(mimetype,remove_bom) 文件" C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ pydrive \ files.py",第43行,_decorated return decoratee(self,* args,** kwargs) 文件" C:\ Python27 \ ArcGIS10.4 \ lib \ site-packages \ pydrive \ files.py",第265行,在FetchContent中 '没有在元数据中找到的mimetype的downloadLink / exportLinks') FileNotDownloadableError:没有在元数据中找到的mimetype的downloadLink / exportLinks
有什么建议吗?
import pydrive
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.LoadCredentialsFile(r"C:\Users\XXXXX\.credentials\drive-python-quickstart.json")
drive = GoogleDrive(gauth)
print "Auth Success"
folder_id = '0BxbuUXtrs7adSFFYMG0zS3VZNFE'
lister = drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
for item in lister:
print item['title']
item.GetContentFile(r'C:\Users\XXXXX\Desktop\googedrive\\' + item['title'])
答案 0 :(得分:5)
您可能正在尝试下载Google文档,电子表格或其他不是普通文件的内容。
我刚刚尝试下载mimeType:application / vnd.google-apps.document文件时出现了完全相同的错误。在下载之前,必须将文档导出为其他格式。 检查一下:
import pydrive
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.LoadCredentialsFile(r"C:\Users\XXXXX\.credentials\drive-python- quickstart.json")
drive = GoogleDrive(gauth)
print "Auth Success"
folder_id = '0BxbuUXtrs7adSFFYMG0zS3VZNFE'
lister = drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
for item in lister:
print(item['title'])
# this should tell you which mimetype the file you're trying to download
# has.
print('title: %s, mimeType: %s' % (item['title'], item['mimeType']))
mimetypes = {
# Drive Document files as PDF
'application/vnd.google-apps.document': 'application/pdf',
# Drive Sheets files as MS Excel files.
'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
# see https://developers.google.com/drive/v3/web/mime-types
}
download_mimetype = None
if file['mimeType'] in mimetypes:
download_mimetype = mimetypes[file['mimeType']]
file.GetContentFile(file['title'], mimetype=download_mimetype)
item.GetContentFile(r'C:\Users\XXXXX\Desktop\googedrive\\' + item['title'], mimetype=download_mimetype)
else:
item.GetContentFile(r'C:\Users\XXXXX\Desktop\googedrive\\' + item['title'])
这应该有效。
答案 1 :(得分:0)
将文档上传到Google驱动器时,请确保在Google驱动器设置中未选中“将文档转换为Google文档编辑器格式”选项。这样可以确保您上传的文件与本地文件的格式相同(例如.csv,.txt等)。对我来说,这很有效。
答案 2 :(得分:0)
在我的情况下,是因为有多个文件具有相同的名称和扩展名但具有不同的ID,所以我删除了一个文件,并且代码起作用了
答案 3 :(得分:0)
从此线程的示例中构建,这里是一个脚本,该脚本遍历gdrive文件夹的所有子文件夹,并下载文件夹结构和其中的所有文件。负责gdrive文档,电子表格和演示文稿。记录文件是否失败:)
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
MIMETYPES = {
# Drive Document files as MS dox
'application/vnd.google-apps.document': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
# Drive Sheets files as MS Excel files.
'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
# Drive presentation as MS pptx
'application/vnd.google-apps.presentation': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
# see https://developers.google.com/drive/v3/web/mime-types
}
EXTENSTIONS = {
'application/vnd.google-apps.document': '.docx',
'application/vnd.google-apps.spreadsheet': '.xlsx',
'application/vnd.google-apps.presentation': '.pptx'
}
f = open("failed.txt","w+")
folder_id = '<folder_id_from_browser_address_bar>'
root = 'drive_download'
os.mkdir(root)
def escape_fname(name):
return name.replace('/','_')
def search_folder(folder_id, root):
file_list = drive.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
for file in file_list:
# print('title: %s, id: %s, kind: %s' % (file['title'], file['id'], file['mimeType']))
# print(file)
if file['mimeType'].split('.')[-1] == 'folder':
foldername = escape_fname(file['title'])
create_folder(root,foldername)
search_folder(file['id'], '{}{}/'.format(root,foldername))
else:
download_mimetype = None
filename = escape_fname(file['title'])
filename = '{}{}'.format(root,filename)
try:
print('DOWNLOADING:', filename)
if file['mimeType'] in MIMETYPES:
download_mimetype = MIMETYPES[file['mimeType']]
file.GetContentFile(filename+EXTENSTIONS[file['mimeType']], mimetype=download_mimetype)
else:
file.GetContentFile(filename)
except:
print('FAILED')
f.write(filename+'\n')
def create_folder(path,name):
os.mkdir('{}{}'.format(path,escape_fname(name)))
search_folder(folder_id,root+'/')
f.close()