缩进错误python 2.7

时间:2018-01-04 04:02:25

标签: python python-2.7 indentation nano

您好我在python 2.7上有缩进错误 我的程序看起来像这样: 进口:
import sys, os import subprocess from threading import Thread from re import split

实际代码:
def GetProcesses(Clean=True): # if Clean == True: # # x = subprocess.Popen(["sudo", "ps", "aux"], stdout=subprocess.PIPE, shell=True) (out, err) = x.communicate() print(out) print(out.split("\n")) print("--------------") Processes = out.split("\n") print(Processes) print("------") print(Processes[0]) print("----------") Header = Processes[0] Process_list = Processes.remove(Processes[0]) return((Header, Process_list)) # else: # # if Clean == True: #added problem so future SE users can see it x = subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE, shell=True) (out, err) = x.communicate() return(out)

我不理解错误,我试过去除每行1个空格,缩进原始版本,并添加1个空格,但它总是说它要么是缩进,要么是意外的缩进。 P.S我只使用空格。

实际错误:
File "MemoryHandling.py", line 31 x = subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE, shell=True) ^ IndentationError: expected an indented block


我在询问此问题期间和之前检查了几个在线问题/来源(主要是SE)的错误(下面),我发现它们是特定于案例,投票/未回答,或者没有用处:
- Python Indentation Error#我正在使用空格
- Python 2.7 Indentation error#我多次手动检查缩进,并尝试使用tab键
- Unexpected indentation error, but indentation looks correct#再次不使用制表符和空格
- Code Indent Error, code is indented?#没有回答(错误?)
- Python Indentation Error when there is no indent error#再次使用制表符和空格

2 个答案:

答案 0 :(得分:1)

import sys, os
import subprocess
from threading import Thread
from re import split

#Actual Code:
def GetProcesses(Clean):
    #
    if Clean == True:
        #
        #
        x = subprocess.Popen(["sudo", "ps", "aux"], stdout=subprocess.PIPE, shell=True)
        (out, err) = x.communicate()
        print(out)
        print(out.split("\n"))
        print("--------------")
        Processes = out.split("\n")
        print(Processes)
        print("------")
        print(Processes[0])
        print("----------")
        Header = Processes[0]
        Process_list = Processes.remove(Processes[0])
        return((Header, Process_list))
    #
    else:
        #
        #
        x = subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE, shell=True)
        (out, err) = x.communicate()
        return(out)

答案 1 :(得分:1)

问题出在这里。在if部分之后,没有提供缩进。

else:
    if Clean == True: #added problem so future SE users can see it
        x = subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE, shell=True)
        (out, err) = x.communicate()
        return(out)