4

I am working on a project to run PEP8 style check on local projects. I’ve tried to use the subprocess method and I am able to get the generated terminal output of the tips to improve style and save it to a string.

My code to generate the PEP8 style is as below:

def run_pep8_style(target):
    pep_tips = subprocess.Popen("python pep8.py --ignore=E111,E501 --filename=*.py " + target, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    tips = pep_tips.communicate()[0]

    print "Style: "
    print tips

However, I have tried to generate the count using the same subprocess method and store but I am unsuccessful of doing it. The terminal display the output but it is not captured into the string variable.

def run_pep8_count(target):
    pep_tips = subprocess.Popen("python pep8.py --ignore=E111,E501 --count -qq --filename=*.py  " + target, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    tips = pep_tips.communicate()[0]
    print "Count: "
    print tips

The strange thing is that I am able to store the list of styles from terminal text into a string variable but it returns none when I try to capture the PEP8 count. Is the terminal output of the count differs from the list of styles? I am a novice to Python programming so any help would be appreciated. Thanks.

1
  • never used that pep8.py script you are using, but check that it prints to stdout and not to stderr. Also, maybe you have to wait until the process finishes so that its output is flushed and you can read it. Commented Jul 7, 2011 at 12:36

1 Answer 1

2

According to the docs, pep8.py --count prints to stderr:

--count              print total number of errors and warnings to standard
                       error and set exit code to 1 if total is not null

So you need to tell subprocss.Popen to direct stderr to subprocess.PIPE:

pep_tips = subprocess.Popen("python pep8.py --ignore=E111,E501 --count -qq          
               --filename=*.py  " + target, shell=False,
               stdin=subprocess.PIPE, stdout=subprocess.PIPE, 
               stderr=subprocess.PIPE)

tips,tips_err = pep_tips.communicate()
print tips_err
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, your solution works perfectly in my case. Thank you. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.