运行脚本时出现 Python 错误 - “IndentationError: unindent does not match any external indentation”
脚本代码片段时出现错误
Error:"IndentationError: unindent does not match any outer indentation"
当我尝试运行引发错误的
def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
print "[-] Error: Check your phpvuln.txt path and permissions"
print "[-] Update Failed\n"
sys.exit(1)
try:
:这是发生错误的实际行:
print "[-] Update Failed\n"
I'm getting an error when I try to run my script
Error:"IndentationError: unindent does not match any outer indentation"
Code snipet that throws the error:
def update():
try:
lines = open("vbvuln.txt", "r").readlines()
except(IOError):
print "[-] Error: Check your phpvuln.txt path and permissions"
print "[-] Update Failed\n"
sys.exit(1)
try:
This is the actual line that occurs the error:
print "[-] Update Failed\n"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在
sys.exit(1)
之前添加一个空格,或者在print "[-] Error: Check your phpvuln.txt path and requests"
和print " 之前删除空格[-] 更新失败\n"
。Put a space before
sys.exit(1)
or remove space beforeprint "[-] Error: Check your phpvuln.txt path and permissions"
andprint "[-] Update Failed\n"
.正如其他人提到的,您需要确保每个代码块具有完全相同的缩进。
他们没有提到的是,广泛采用的约定始终是每个缩进正好使用 4 个空格。 在您的代码中,
print
语句使用 5 个空格缩进(很可能是意外)。因此,请勿向sys.exit(1)
添加另一个空格; 从print
语句中删除空格。修改后的代码:
As others have mentioned, you need to make sure that each code block has the exact same indentation.
What they haven't mentioned is that the widely adopted convention is to always use exactly 4 spaces per indentation. In your code, the
print
statements are indented using 5 spaces (most likely by accident.) So do not add another space tosys.exit(1)
; remove the spaces from theprint
statements.Revised code:
保持缩进标准的一个好方法是使用 Tab 键而不是空格键。
A good way to maintain a standard for your indentations is to use the tab key instead of spacebar.
你有一个空的 try 块,下面什么都没有。 这是导致错误的原因。
顺便说一句,您的 sys.exit(1) 也没有缩进。 在Python中,缩进很重要,因为这是Python解释器确定代码块的方式。 因此,您必须正确缩进代码才能运行代码。
You have an empty try block with nothing underneath it. This is causing the error.
BTW, your
sys.exit(1)
is off-indent as well. In python, Indentation is important because this is how Python interpreter determines code blocks. So, you have to indent your code properly to get your code running.缩进错误发生在您缩进不正确的地方。 从“sys”开头的行中有不同的缩进这一事实可以清楚地看出这一点。
The indentation error happens where you have indented incorrectly. Which is clearly visible by the fact that you have different indentation in the line starting with "sys".