IndexError错误错误Python

67

我正在删除Slack中的幻象文件。下面是我的脚本。我收到IndexError列表索引超出范围错误。有人可以针对我做错的事情提供帮助。

import Slacker
import sys
import time
from datetime import timedelta, datetime


def main(token, weeks=4):
    slack = Slacker(token)
    total = slack.files.list(count=1).body['paging']['total']
    num_pages = int(total/1000.00 + 1)
    print("{} files to be processed, across {} pages".format(total, num_pages))
    files_to_delete = []
    ids = [] 
    count = 1
    for page in range(num_pages):
        print ("Pulling page number {}".format(page + 1))
        files = slack.files.list(count=1000, page=page+1).body['files']
        for file in files:
            print("Checking file number {}".format(count))
            if file['id'] not in ids:
                ids.append(file['id'])
                if datetime.fromtimestamp(file['timestamp']) < datetime.now() - timedelta(weeks=weeks):
                    files_to_delete.append(file)
                    print("File No. {} will be deleted".format(count))
                else:
                    print ("File No. {} will not be deleted".format(count))
            count+=1

    print("All files checked\nProceeding to delete files")
    print("{} files will be deleted!".format(len(files_to_delete)))
    count = 1
    for file in files_to_delete:
        print("Deleting file {} of {}".format(count, len(files_to_delete)))
        slack.files.delete(file_=file['id'])
        print("Deleted Successfully")
        count+=1

    return count-1

if __name__ == "__main__":
    try:
        token = sys.argv[1:]
    except IndexError:
        print("Usage: python file_deleter.py api_token\nPlease provide a value for the API Token")
        sys.exit(2)

    main(token[0])

它在最后一行主行(令牌[0])上给出错误。

Traceback (most recent call last):
  File "C:/Users/aqureshi/Desktop/Programming/Slacker.py", line 50, in <module>
    main(token[0])
IndexError: list index out of range
板球运动员

您可以像这样解决错误,但这只会告诉您如何实际运行此文件。

换句话说,请提供API令牌的值

if __name__ == "__main__":
    try:
        token = sys.argv[1:]
        main(token[0])
    except IndexError:
        print("Usage: python file_deleter.py api_token\nPlease provide a value for the API Token")
        sys.exit(2)

就像是 Desktop\> python Slacker.py xxxxAPI_TOKENxxxx

我可能会重写该try-except来实际验证参数的长度

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python file_deleter.py api_token\nPlease provide a value for the API Token")
        sys.exit(2)

    token = sys.argv[1:]
    main(token[0])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python错误:IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围错误python

来自分类Dev

Python 请求给出错误:IndexError: list index out of range

来自分类Dev

Python,列表错误:IndexError:列表索引超出范围

来自分类Dev

python中的“IndexError:列表索引超出范围”错误

来自分类Dev

渲染模板时出现Django错误IndexError

来自分类Dev

Python错误:IndexError:图片索引超出范围(im.putpixel)

来自分类Dev

如何传递错误IndexError:单个位置索引器在python中越界

来自分类Dev

Python-错误:lib_type =字段[4] IndexError:列表索引超出范围

来自分类Dev

IndexError:列表索引超出范围-在Python中抛出错误?

来自分类Dev

Python 错误中的 OLS 滚动回归 - IndexError:索引超出范围

来自分类Dev

如何解决python中的“IndexError:字符串索引超出范围”错误?

来自分类Dev

Python:IndexError:列表索引超出范围(无法定位错误)

来自分类Dev

如何解决python中出现以下错误:“IndexError: string index out of range”

来自分类Dev

python 代码错误(IndexError: list assignment index out of range)初学者级别

来自分类Dev

如何修复python中的“IndexError('切片停止%d超过%d'%(停止,长度))的限制”错误

来自分类Dev

错误:IndexError ::列表索引超出范围aws部署错误

来自分类Dev

Python错误

来自分类Dev

python错误

来自分类Dev

如何修复 Python 上 p2p 聊天应用程序的“IndexError: list index out of range”错误?

来自分类Dev

熊猫错误:“ IndexError:iloc无法扩大其目标对象”

来自分类Dev

IndexError:通过for循环列出索引超出范围的错误

来自分类Dev

IndexError:字符串索引超出范围错误

来自分类Dev

IndexError:范围对象索引超出范围错误

来自分类Dev

Python dateutil错误规则错误

来自分类Dev

Python glutCreateWindow错误“类型错误”

来自分类Dev

可能从 0 到 X 计数,从 1 到 x 计数给出错误“IndexError:列表分配索引超出范围”python

来自分类Dev

Python直方图错误栏

来自分类Dev

填充python字典错误

Related 相关文章

  1. 1

    python错误:IndexError:列表索引超出范围

  2. 2

    IndexError:列表索引超出范围错误python

  3. 3

    Python 请求给出错误:IndexError: list index out of range

  4. 4

    Python,列表错误:IndexError:列表索引超出范围

  5. 5

    python中的“IndexError:列表索引超出范围”错误

  6. 6

    渲染模板时出现Django错误IndexError

  7. 7

    Python错误:IndexError:图片索引超出范围(im.putpixel)

  8. 8

    如何传递错误IndexError:单个位置索引器在python中越界

  9. 9

    Python-错误:lib_type =字段[4] IndexError:列表索引超出范围

  10. 10

    IndexError:列表索引超出范围-在Python中抛出错误?

  11. 11

    Python 错误中的 OLS 滚动回归 - IndexError:索引超出范围

  12. 12

    如何解决python中的“IndexError:字符串索引超出范围”错误?

  13. 13

    Python:IndexError:列表索引超出范围(无法定位错误)

  14. 14

    如何解决python中出现以下错误:“IndexError: string index out of range”

  15. 15

    python 代码错误(IndexError: list assignment index out of range)初学者级别

  16. 16

    如何修复python中的“IndexError('切片停止%d超过%d'%(停止,长度))的限制”错误

  17. 17

    错误:IndexError ::列表索引超出范围aws部署错误

  18. 18

    Python错误

  19. 19

    python错误

  20. 20

    如何修复 Python 上 p2p 聊天应用程序的“IndexError: list index out of range”错误?

  21. 21

    熊猫错误:“ IndexError:iloc无法扩大其目标对象”

  22. 22

    IndexError:通过for循环列出索引超出范围的错误

  23. 23

    IndexError:字符串索引超出范围错误

  24. 24

    IndexError:范围对象索引超出范围错误

  25. 25

    Python dateutil错误规则错误

  26. 26

    Python glutCreateWindow错误“类型错误”

  27. 27

    可能从 0 到 X 计数,从 1 到 x 计数给出错误“IndexError:列表分配索引超出范围”python

  28. 28

    Python直方图错误栏

  29. 29

    填充python字典错误

热门标签

归档