运行脚本时出现 Python 错误 - “IndentationError: unindent does not match any external indentation”

发布于 2024-07-27 03:00:02 字数 474 浏览 3 评论 0原文

脚本代码片段时出现错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星光不落少年眉 2024-08-03 03:00:02

sys.exit(1) 之前添加一个空格,或者在 print "[-] Error: Check your phpvuln.txt path and requests"print " 之前删除空格[-] 更新失败\n"

Put a space before sys.exit(1) or remove space before print "[-] Error: Check your phpvuln.txt path and permissions" and print "[-] Update Failed\n".

淡淡的优雅 2024-08-03 03:00:02

正如其他人提到的,您需要确保每个代码块具有完全相同的缩进。

他们没有提到的是,广泛采用的约定始终是每个缩进正好使用 4 个空格。 在您的代码中,print 语句使用 5 个空格缩进(很可能是意外)。因此,请勿向 sys.exit(1) 添加另一个空格; 从 print 语句中删除空格。

修改后的代码:

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)

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 to sys.exit(1); remove the spaces from the print statements.

Revised code:

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)
匿名。 2024-08-03 03:00:02

保持缩进标准的一个好方法是使用 Tab 键而不是空格键。

A good way to maintain a standard for your indentations is to use the tab key instead of spacebar.

孤独患者 2024-08-03 03:00:02

你有一个空的 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.

梦境 2024-08-03 03:00:02

缩进错误发生在您缩进不正确的地方。 从“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文