-1

Hello I am having an indentation error on python 2.7
My program looks like this: Imports:
import sys, os import subprocess from threading import Thread from re import split

Actual Code:
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

  • did you try running with `python -tt yourscript.py`? This will throw an error if you've accidentally mixed tabs and spaces. – mgilson Jan 04 '18 at 04:03
  • After `if Clean == True:` you seem to be missing tabs (probably on the next 3 lines after that statement) – Savir Jan 04 '18 at 04:04
  • It does throw an error, but i have not used tab... i will re - write it – brian waltse Jan 04 '18 at 04:04
  • tabs or whitespaces... What I mean is that the `x = subprocess...` has to be indented (can't be at the same level than the `if`) – Savir Jan 04 '18 at 04:06
  • 1
    However, that code doesn't make sense, since the first line of the `GetProcess` function already checks `if Clean == True`. Then you have an `else` (which will only be hit if `Clean` is `False`) and within that, you check again `if Clean == True`, which we already know it's not gonna happen because you already checked it on the top of your function – Savir Jan 04 '18 at 04:07
  • Indentation errors aside, why do you have `if Clean == True` followed by `else: if Clean == True`? That makes no logical sense. – John Gordon Jan 04 '18 at 04:08
  • yes that worked.... the cringe is real........ how do i mark an answer as a comment – brian waltse Jan 04 '18 at 04:11
  • You have to wait for somebody to post the comment as an answer. Also, instead of using the ` character a bunch of times, you can start each line you want to be a contiguous code block with 4 spaces if you would like. – Hans Musgrave Jan 04 '18 at 04:12
  • oh thank you i added the clean == true for debugging i dont know why... – brian waltse Jan 04 '18 at 04:15
  • 1
    `i found that they were case specific, downvoted / not answered, or were not useful` - I think you can see why! –  Jan 04 '18 at 04:20

2 Answers2

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)
Rohit-Pandey
  • 2,039
  • 17
  • 24
1

The problem is here. After if part, there is no indentation provided.

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)
Heenashree Khandelwal
  • 659
  • 1
  • 13
  • 30