0

I have a file named phone.py which give me output as(in terminal):

+911234567890
+910123321423

There can be more number of outputs.

Another file named email.py which produces(in terminal):

and@abc.com
bcd@cdc.com

or more.

And I have a JSON File whose structure is as follows:

{"One":"Some data",
 "two":"Some more data",
 "three":"Even more data"}

There can be a many more sections like this.

Now I want the capture the terminal output and also load the existing JSON and finally, have an output as follows (as a JSON file):

{Phone:"+911234567890,+910123321423", "Email":"and@abc.com,bcd@cdc.com","Sections":"{"One":"Some data",
 "two":"Some more data",
 "three":Even more data"}"}

I tried to capture the output using subprocess module in python and now it is stored in a variable

subprocess.run(['python','email.py','filename.txt'], stdout= subprocess.PIPE)

output:

CompletedProcess(args=['python', 'email_txt.py', 'upload/filename.txt'], returncode=0, stdout=b'abc@xyz.com\nbcd@dcd.com\n')

I have a string in which data is stored not I want the desired output through these components. What can I do or refer to tackle this problem ?

anshaj
  • 293
  • 1
  • 6
  • 24

2 Answers2

3

You can obtain the stdout from subprocess.run simply by resp.stdout, where resp is the returned object.

xrisk
  • 3,790
  • 22
  • 45
  • Maybe also mention that the optional argument `universal_newlines=True` will also take care of decoding any output according to the default terminal encoding. – tripleee Apr 06 '17 at 10:12
0

As already mentioned by Rishav, you need to assign the output to a variable & then use it to get the related attributes.

Sample usage -

>>> import subprocess
>>> out = subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
>>> out.stdout
b'crw-rw-rw- 1 root root 1, 3 Apr  6  2017 /dev/null\n'
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90