Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Getting decode error.
#1
import sys
import os
import subprocess
import inspect

def run_process(cmd_args):
        with subprocess.Popen(cmd_args) as p:
            p.communicate()

if __name__ == "__main__":
    f = open(os.environ['OUTPUT_PATH'], 'w')

    cmd_args_cnt = 0
    cmd_args_cnt = int(input())
    cmd_args_i = 0
    cmd_args = []
    while cmd_args_i < cmd_args_cnt:
        try:
            cmd_args_item = str(input())
        except:
            cmd_args_item = None
        cmd_args.append(cmd_args_item)
        cmd_args_i += 1


    res = run_process(cmd_args);
    #f.write(res.decode("utf-8") + "\n")
    
   
    
    if 'with' in inspect.getsource(run_process):
        f.write("'with' used in 'run_process' function definition.\n")
    
    if 'Popen' in inspect.getsource(run_process):
        f.write("'Popen' used in 'run_process' function definition.\n")
        f.write('Process Output : %s\n' % (res.decode("utf-8")))

    f.close()
# this is the input
3
python
-c
print("Hello")
Your Output
'with' used in 'run_process' function definition.
'Popen' used in 'run_process' function definition.

Expected Output
'with' used in 'run_process' function definition.
'Popen' used in 'run_process' function definition.
Process Output : Hello

ERROR(stderr):
Error:
Traceback (most recent call last): File "solution.py", line 46, in <module> f.write('Process Output : %s\n' % (res.decode("utf-8"))) AttributeError: 'NoneType' object has no attribute 'decode'
buran wrote Sep-13-2018, 07:30 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
your run_process function does not have explicit return, thus it returns None. It look like tou want to return something and it has to have decode method
Quote
#3
I am new to forum.Thanks for adding tags.
I tried this code
def run_process(cmd_args):
    with subprocess.Popen(cmd_args) as p:
        p.communicate()
    return p
getting this error
Error:
Traceback (most recent call last): File "solution.py", line 44, in <module> f.write('Process Output : %s\n' % (res.decode("utf-8"))) AttributeError: 'Popen' object has no attribute 'decode'
Quote
#4
Like this and you most also activate stdout,stderr pipes.
Now you decode out.decode() is the same as out.decode('utf-8')
def run_process(cmd_args):
    with subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
        out, err = p.communicate()
    return out
If also want to return err return out,err,then it become a Tuple and you use res[0].decode().

This is a very old way of doing string formatting.
From 3.6 use f-string.
f.write('Process Output : %s\n' % (res.decode("utf-8"))).
# To
f.write(f'Process Output: {res.decode()}\n')
Quote
#5
This line is computer generated.I cant modify it
      f.write('Process Output : %s\n' % (res.decode("utf-8")))
I tried your function but I got this error
Error:
Traceback (most recent call last): File "solution.py", line 44, in <module> f.write('Process Output : %s\n' % (res.decode("utf-8"))) AttributeError: 'tuple' object has no attribute 'decode'
Quote
#6
(Sep-13-2018, 09:48 AM)shankar Wrote: This line is computer generated.I cant modify it
      f.write('Process Output : %s\n' % (res.decode("utf-8")))
I tried your function but I got this error
Error:
Traceback (most recent call last): File "solution.py", line 44, in <module> f.write('Process Output : %s\n' % (res.decode("utf-8"))) AttributeError: 'tuple' object has no attribute 'decode'

process.communicate returns a tuple - regular stdout and stderr. Use its result as @snippsat has shown. You are trying to use both elements of the method output.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#7
Can test function.
# cm.py
import subprocess

def run_process(cmd_args):
    with subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as p:
        out, err = p.communicate()
    return out
E:\div_code\new
λ ptpython -i cm.py
>>> res = run_process('ls')

# Return bytes
>>> res
b'001.png\n002.png\n003.png\n004.png\ncm.py\n'

# To string
>>> res.decode()
'001.png\n002.png\n003.png\n004.png\ncm.py\n'

# Write to file
>>> with open('output.txt', 'w') as f:
...     f.write(f'Process Output: \n{res.decode()}\n')
output.txt:
Output:
Process Output: 001.png 002.png 003.png 004.png cm.py
If return out, err then it will be a tuple.
>>> res
(b'001.png\n002.png\n003.png\n004.png\ncm.py\noutput.txt\n', b'')

>>> res[0]
b'001.png\n002.png\n003.png\n004.png\ncm.py\noutput.txt\n'

>>> res[0].decode()
'001.png\n002.png\n003.png\n004.png\ncm.py\noutput.txt\n'

# Or unpack back
>>> out, err = res

>>> out
b'001.png\n002.png\n003.png\n004.png\ncm.py\noutput.txt\n'
Quote
#8
Thanks a lot @snippsat
Quote
#9
Shankar, the question you posted is for an exercise from Hackerrank and should not be shared publicly.

I hope you passed the course.

tinman
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  charmap codec can't decode byte error with gzipped file in python bluethundr 2 311 Apr-30-2019, 12:26 PM
Last Post: bluethundr
  decode base64 with python give error thailq 3 763 Sep-24-2018, 12:39 AM
Last Post: thailq
  help coding for decode filtering dony71 3 676 Jun-29-2018, 08:44 PM
Last Post: volcano63
  how to decode UTF-8 in python 3 oco 3 14,807 Jun-05-2018, 11:05 AM
Last Post: wavic
  Ask help for utf-8 decode/encode forfan 12 3,685 Feb-25-2017, 02:04 AM
Last Post: forfan

Forum Jump:


Users browsing this thread: 1 Guest(s)