尝试为 bash 脚本调整基本分析器,如下代码块。
无法弄清楚为什么使用“if 分支”分析代码会引发异常(例如,使用重复文件运行此 code 时)。根据我的追踪,它在某处创建了一个额外的索引值。如果这是微不足道的,我很抱歉,因为我是 Python 新手,但是任何有关问题出在哪里或如何解决问题的建议将不胜感激
def collect_timings(profiled_line, i):
if i == len(results) - 1:
return [0] + profiled_line
timing = float(results[i+1][1].replace(".N", "")) - float(profiled_line[1].replace(".N", ""))
return [timing] + profiled_line
错误:
Traceback (most recent call last):
File "./profile", line 67, in <module>
main(sys.argv)
File "./profile", line 51, in main
profiling_time = map(collect_timings, results, range(len(results)))
File "./profile", line 24, in collect_timings
timing = float(results[i+1][1].replace(".N", "")) - float(profiled_line[1].replace(".N", ""))
IndexError: list index out of range
最佳答案
找到答案,发布以防万一它对某人有用。 问题在于输出重定向:
echo "Found '$file' is a duplicate of '${filecksums[$cksum]}'" >&2
它作为一个单独的条目传递给结果
:
["Found 'txt2/0122file.bck' is a duplicate of 'txt2/0113file.txt'\n"]
不知道如何使代码通过重定向运行,但这不是必需的,所以我会避免它。
关于Python - 索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054450/