【问题标题】:youtube api upload file: invalid syntax on except HttpError, eyoutube api 上传文件:除了 HttpError,e 之外的语法无效
【发布时间】:2016-04-14 21:18:35
【问题描述】:

我的python版本是3.5.1,尝试运行这段代码https://developers.google.com/youtube/v3/code_samples/python#upload_a_video 修复了过时的打印调用,现在它给了我 SyntaxError: invalid syntax error

def resumable_upload(insert_request):
  response = None
  error = None
  retry = 0
  while response is None:
    try:
      print ("Uploading file...")
      status, response = insert_request.next_chunk()
      if 'id' in response:
        print ("Video id '%s' was successfully uploaded." % (response['id']))
      else:
        exit("The upload failed with an unexpected response: %s" % response)

    except HttpError, e: //error here pointed at comma

      if e.resp.status in RETRIABLE_STATUS_CODES:
        error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
                                                             e.content)
      else:
        raise
    except RETRIABLE_EXCEPTIONS, e:
      error = "A retriable error occurred: %s" % e

    if error is not None:
      print error
      retry += 1
      if retry > MAX_RETRIES:
        exit("No longer attempting to retry.")

      max_sleep = 2 ** retry
      sleep_seconds = random.random() * max_sleep
      print ("Sleeping %f seconds and then retrying..." % (sleep_seconds)
      time.sleep(sleep_seconds)

我是一个完整的 python 新手,用谷歌搜索可能是因为我错过了“尝试”,但它就在那里。

因为这个YouTube API v3 upload speeds尝试python

这个错误会不会是因为我的版本比库支持的更新? https://developers.google.com/api-client-library/python/start/installation?authuser=1

【问题讨论】:

    标签: python youtube google-api youtube-api syntax-error


    【解决方案1】:

    您使用的是 Python 2 语法,它不适用于 Python 3。

    改用这个:

    except HttpError as e:
    

    【讨论】:

    • 最后一个问题:time.sleep(sleep_seconds) 给出了 SyntaxError: invalid syntax error pointing at first letter e,我读过 Python 3.5 中更改了时间函数,这是一种新的制作方法程序等待 x 秒?
    • 问题出在前一行。 print() 调用没有右括号。
    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 2021-11-20
    • 2021-01-04
    • 2015-01-08
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2017-09-11
    相关资源
    最近更新 更多