Python Forum
A question about subprocess taking input from command line and returning output!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A question about subprocess taking input from command line and returning output!
#1
Dear Python forum members,

The script I have written below (the_script.py) works fine with this terminal command at MAC OS Mojave Python 2.7:
Quote:python the_script.py < ABC.pdb > ABC.txt
! However I want my script (the_script.py below) written through terminal to be like this:
Quote:python the_script.py ABC.pdb
!
And not involve < or > and return the output to ABC.txt. The .pdb file is a text file as well.

#!/usr/bin/python2

import sys

import subprocess

process = subprocess.Popen(
		["voronota","get-balls-from-atoms-file","--annotated"],stdin=subprocess.PIPE,
		stdout=subprocess.PIPE)

output=process.communicate(sys.stdin.read())[0];

print output
I will look forward to all the replies.

Sincerely,
Aurimas
Reply
#2
Replace sys.stdin.read() with pathlib.Path('ABC.pdb').read_text() and replace print(output) with pathlib.Path('ABC.txt').write_text(output). Use the argparse module to get the file names from the command line.
Aurimas Wrote:The script I have written below
In 2019, use python 3.
Reply
#3
Don't forget that the built-in print function has a file keyword where to output the text.
In the case where a line has no line-ending/line-seperator, you could use the print funciton.
It adds by default a newline to the end of the string. This could be controlled with the keyword end

In [1]: print?                                                                  
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type:      builtin_function_or_method
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Thank you guys, but I want to specify what pdb file I choose to use (like 1abc.pdb, 1fgh.pdb or other) and the output I need is
Quote: awk '{sum += $3} END {print sum}'
that prints a numerical value to terminal. Full terminal command in linux/mac would be
Quote:cat HS_1abc.pdb | voronota get-balls-from-atoms-file --annotated | voronota calculate-contacts --annotated | voronota query-contacts --inter-residue --match-first 'R<ARG>' --match-second 'c<solvent>' | awk '{sum += $3} END {print sum}'
I also need the program to say what R in --match-second to use (in this case it is ARG but it can have any one of 20 different values). Also for --match-second I need to define up to fours values like c<A,B,C,D>, c<A,B), c<W,C>, c<solvent>, c<A,C,E> or other letters

How could that be possible with pipes and subprocess and communicate?
Reply
#5
I would try something like this
from subprocess import Popen, PIPE

cat = ['cat', 'HS_1abc.pdb']
get = ['voronota', 'get-balls-from-atoms-file', '--annotated']
calc = ['voronota', 'calculate-contacts', '--annotated']
que = [
    'voronota', 'query-contacts', '--inter-residue',
    '--match-first', 'R<ARG>',
    '--match-second', 'c<solvent>']
awk = ['awk', '{sum += $3} END {print sum}']

cat = Popen(cat, stdout=PIPE)
get = Popen(get, stdin=cat.stdout, stdout=PIPE)
calc = Popen(calc, stdin=get.stdout, stdout=PIPE)
que = Popen(que, stdin=calc.stdout, stdout=PIPE)
awk = Popen(awk, stdin=que.stdout, stdout=PIPE, stderr=PIPE)
out, err = awk.communicate()
print(out)
Reply
#6
Thank you. Should the code be run through python3? I have python 2.7 :(
My main struggle is to make an input being asked before running the script as in your given example I give an input as a string which would require me to write hundreds of different .pdb codes. I would like the script to ask first and then give the numerical value. Otherwise I am better doing it through terminal which I am doing now. Perhaps I need to include dictionary where I could run the calculations :-)
Reply
#7
Aurimas Wrote:I have python 2.7 :(
It is a matter of a few seconds to install a better python.
Reply
#8
Installed Python3 but it works with 2.7 as well. How I could write in terminal: python script 1abc.pdb press enter and get:
XXXXXX - the sum of third columns values
With the written code I get the sum but have to change script all the time to get the correct value, which is wasteful as I have a lot of arguments to change (in ten hundreds or more) :/
Also how I could make 8 line of your proposed code to ask the input for 'R<AAA>' where the script asks to enter AAA as ARG, ASN, ASP, GLN, GLY or any other amino acid three digit code.
Reply
#9
I am tryin to think of how to ask an input to be a text file. It can be any .pdb file like 1abc.pdb, 2abc.pdb, 3drt.pdb, etc.
I am working with upgrading the above script to this:
from subprocess import Popen, PIPE

file = raw_input("> ")
read_file = file.format
open_file = open('read_file', 'r+') 

cat = ['cat', 'open_file']
get = ['voronota', 'get-balls-from-atoms-file', '--annotated']
calc = ['voronota', 'calculate-contacts', '--annotated']
que = [
    'voronota', 'query-contacts', '--inter-residue',
    '--match-first', 'R<ARG>',
    '--match-second', 'c<solvent>']
awk = ['awk', '{sum += $3} END {print sum}']
 
cat = Popen(cat, stdout=PIPE)
get = Popen(get, stdin=cat.stdout, stdout=PIPE)
calc = Popen(calc, stdin=get.stdout, stdout=PIPE)
que = Popen(que, stdin=calc.stdout, stdout=PIPE)
awk = Popen(awk, stdin=que.stdout, stdout=PIPE, stderr=PIPE)
out, err = awk.communicate()
print(out)
but am getting this error:
Error:
Traceback (most recent call last): File "voronota_script.py", line 4, in <module> open_file = open('read_file', 'r+') IOError: [Errno 2] No such file or directory: 'read_file'
How is it possible to ask an input to be a txt file (in my case x.pdb where x has 28 different values)?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning numeral output in VS code terminal StephCreatesCode 4 218 Jan-26-2026, 03:45 PM
Last Post: DeaD_EyE
  I am a newbie I like to use the command line Mikel2025 1 1,063 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,237 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
  interactive process for input and output maiya 1 2,300 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  How to revert back to a previous line from user input Sharkenn64u 2 3,184 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 1,828 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
Question [SOLVED] Same input different output antarling 2 1,553 Oct-25-2024, 11:28 PM
Last Post: antarling
  I think I need to delete input data because returning to start fails thelad 2 1,716 Sep-24-2024, 10:12 AM
Last Post: thelad
  Simplest way to run external command line app with parameters? Winfried 2 2,360 Aug-19-2024, 03:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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