我正在删除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] 删除。
我来说两句