Search code examples
pythonpython-2.7indentationnano

Why did I get this indentation Error in python 2.7?


Hello I am having an indentation error on python 2.7
My program looks like this:

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)

I am not understanding the error, I have tried de-denting everyline 1 space, indenting back to the originals, and adding 1 space but it always says that it either expects an indent, or unexpected indent. P.S I am using only spaces.

Actual Error:

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

I have check several online questions/sources (mainly SE) for this error (below) both during and before asking this question, i found that they were case specific, downvoted / not answered, or were not usefull:
-- Python Indentation Error # i am using spaces
-- Python 2.7 Indentation error # i have manually checked indentation multiple times, and tried using the tab key
-- Unexpected indentation error, but indentation looks correct # once again not using tabs and spaces
-- Code Indent Error, code is indented? # not answered (bug?)
-- Python Indentation Error when there is no indent error # once more not using tabs and spaces


Solution

  • 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)