Getting extra b' while calling external python script.

What “error”? I see no error here.

(Side note: showing a couple lines of code outside the context in which they’re being used doesn’t help. Please post the complete function.)

Not any error, even after using decode to UTF, I’m getting same “b’” in output.

Please post the complete function that you’re currently using.

Please check the details in url

I don’t see where you have the decode method being used in this.

You only need to post your updated file, if that’s what you’re concerned about.

Also, for the long-term viability of the information being posted here, please post the code here - don’t link to external sources.

In beginning, I tried to post all code here only but it was not coming in proper format thats why I used google doc.

Now, I’m not getting any output except b"

import sys
import datetime
result=“Hi %s current time is %s” % (sys.argv[1],time)

Ahh - when posting code here, enclose each file’s contents between lines of three backtick - ` characters. That means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep the code properly formatted. (You can also do this with templates, error messages, html, and anything else you don’t want the forum to format or interpret.)

Try running your script directly from the command line - does it work? Does it throw an exception?

If this only fails when being executed by your view, you might also want to capture stderr from the process and output that as well.

result.decode was throwing syntax error while running through command line. So, I replaced it with # -- coding: utf-8 --

import sys
import datetime
result="Hi %s current time is %s" % (sys.argv[1],time)
# -*- coding: utf-8 -*-

I’m getting correct output while running script through cli

$ python3 Everyone
Hi Everyone current time is 2022-11-24 20:29:00.748037

But I’m still getting extra b’ and \n through django web page

b'Hi Everyone current time is 2022-11-24 14:58:26.119814\n' 

There are two separate issues here:

  • The print statement is adding the newline character. You can get rid of it using the end argument to print. But, if you correct the second issue, it shouldn’t be needed.

  • The output is coming back to your view as a bytestream. It’s in your view that you need to do the decode of the returned string from the script.

Sorry, for bugging you. Actually, I’m new to django

def external(request):
	inp= request.POST.get('param')
	out= run([sys.executable,'//Path//',inp],shell=False,stdout=PIPE)
    # -*- coding: utf-8 -*-
	return render(request,'logs.html',{'data02' :out.stdout})

I tried 2-3 option, but still I’m not able to find the solution

  1. It doesn’t make any difference
    # -*- coding: utf-8 -*-
  2. Script doesn’t worked
from django.template import Template
     t2 = Template('This is a string template.')

This is a comment - perhaps a directive to an IDE or editor, but functionally it doesn’t do anything for you.

If you need to convert the bytestring to a string you would call decode on the bytestring to convert.

Your function is rendering logs.html - what does that template look like?

HTML Template

	<form action="/external/" method="post">
	  {% csrf_token %}
	 Input Text:
	  <input type="text" name="param" required><br><br>
	  <input type="submit" value="Submit">

I feel, do we have something in HTML, where we can define that take input data as str instead of Bytes? Or not sure if we have some different options

The issue isn’t in either your template or your script.

The issue is in using the run command and passing data through the command parameters and stdout. Those interfaces are, by definition, bytestreams. Python has no way of passing or receiving a Python String externally.

So it doesn’t matter what you do - what you send to your script is going to be a bytestream and what you get back is going to come back as a bytestream. It’s up to you to convert that into a string where needed.

In views, I tried all below three but last 2 show syntax error…1st one no difference in output

  • Using [decode()]
  • Using [str()]
  • Using [codecs.decode()]

If you’re looking for specific assistance, you’ll need to provide the code where you tried to use the decode function.


import sys
from subprocess import run,PIPE
import codecs

def external(request):
	inp= request.POST.get('param')
	result= run([sys.executable,'//Path//',inp],shell=False,stdout=PIPE)
    out = codecs.decode(result)
	return render(request,'logs.html',{'data02' :out.stdout})

Here I tried all 3

  • Using [decode()]
  • Using [str()]
  • Using [codecs.decode()]

Output is:-
b’Hi Everyone current time is 2022-11-27 07:40:25.297318’

Want to remove first b…which is in bold.

That’s because you’re trying to decode the result object itself, and not the stdout (the bytestream) from that object.

1 Like

Thanks a lot. Finally it works.

Hi Ken
Could you please take a look of my different issue


Please do not do this. There is no need to post a message asking me to look at another message. I generally see everything coming in, and answer those where I can.