A question about subprocess taking input from command line and returning output! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: A question about subprocess taking input from command line and returning output! (/thread-18249.html) |
A question about subprocess taking input from command line and returning output! - Aurimas - May-10-2019 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 outputI will look forward to all the replies. Sincerely, Aurimas RE: A question about subprocess taking input from command line and returning output! - Gribouillis - May-11-2019 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 belowIn 2019, use python 3. RE: A question about subprocess taking input from command line and returning output! - DeaD_EyE - May-11-2019 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 RE: A question about subprocess taking input from command line and returning output! - Aurimas - May-15-2019 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? RE: A question about subprocess taking input from command line and returning output! - Gribouillis - May-15-2019 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) RE: A question about subprocess taking input from command line and returning output! - Aurimas - May-15-2019 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 :-) RE: A question about subprocess taking input from command line and returning output! - Gribouillis - May-15-2019 Aurimas Wrote:I have python 2.7 :(It is a matter of a few seconds to install a better python. RE: A question about subprocess taking input from command line and returning output! - Aurimas - May-15-2019 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. RE: A question about subprocess taking input from command line and returning output! - Aurimas - May-15-2019 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: How is it possible to ask an input to be a txt file (in my case x.pdb where x has 28 different values)?
|