Python Forum
subprocess return of stdout after running Linux command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess return of stdout after running Linux command
#1
I wrote a function which accepts an argument (a Linux command) and then runs subprocess.check_output

  
#!/usr/local/bin/python3.5
#
#
#  FUNCTIONS
#
def run_command(command):
    p = subprocess.check_output(command, stdin=None, stderr=None, timeout=None, shell=True, universal_newlines=False )
    print('the output value is: ')
    print(p)

#
#
#   END OF FUNCTIONS
#
import subprocess
run_command('pwd')
when invoking this function with the argument 'pwd'  I expected to get a string containing '/home/jdavis'

however, this is what I got for command output:
[jdavis@localhost ~]$ ./ex1
the output value is:
b'/home/jdavis\n'
[jdavis@localhost ~]$


Why do I get the 'b' prepended  and the '\n  at the end?  and how to I prevent this?
Reply
#2
b is bytes. You need to convert it to a string and then remove the newline with strip method
    print(p.decode().strip())
Recommended Tutorials:
Reply
#3
import os

print(os.getcwd())
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
I would have tried that first, but saw in documentation that os  is deprecated  -  I'm running 3.5.2  -  should I be concerned about using the os command?
Reply
#5
Where did you read this? In the Python documentation there is nothing like this.
https://docs.python.org/3/library/os.html
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Oct-13-2016, 05:06 PM)ivanachukapawn Wrote: saw in documentation that os  is deprecated
Link?
Reply
#7
I found the 'deprecated' os  in multiple places (google 'Python os deprecated' .  Here is quote from Kevin London's blog:

The Command Injection Series
The first group of dangerous Python functions revolve around trusting user-provided input and piping it into a shell.
Why they’re useful
Sometimes you need to send a command-line application and it’s convenient to do that using Python’s subprocess module using
subprocess.call()
and setting
shell=True
.
os.system()
and
os.popen*()
have been deprecated since Python 2.6 in favor of the subprocess module. They have a simple API and they’ve been around for a while so you may run into them in older applications.

But I'm not particularly worried about any security concerns via os   and I found it vastly superior to subprocess in terms of simplicity and clean output returned  (apparently, subprocess returns byte output from command stdout)

So I'm going to proceed with os.  Thanks so much for the help.
Reply
#8
The module itself is not deprecated, just specific functions within it. You can use the os module, just avoid the deprecated parts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 720 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  [subprocess] Why stdout sent to stderr? Winfried 3 463 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Using subprocess to execute complex command with many arguments medatib531 5 1,844 Apr-27-2023, 02:23 PM
Last Post: medatib531
  Running script with subprocess in another directory paul18fr 1 3,696 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Running 3rd party libs on Steam Deck (Arch Linux) with restricted access metulburr 0 1,837 Jan-07-2023, 10:41 PM
Last Post: metulburr
  Performance options for sys.stdout.writelines dgrunwal 11 3,107 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  How to use a variable in linux command in python code? ilknurg 2 1,596 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Controlling what get outputted to stdout when running external commands Daring_T 4 2,159 Jan-30-2022, 05:40 PM
Last Post: bowlofred
  use subprocess on linux\pi wwith a "grep " command korenron 2 8,065 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,617 May-04-2021, 08:58 PM
Last Post: liudr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020