How to call bash on Python and put the result to the variable? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How to call bash on Python and put the result to the variable? (/thread-27145.html) |
How to call bash on Python and put the result to the variable? - meknowsnothing - May-27-2020 Could anyone help me to solve the question on https://www.ohjelmointiputka.net/postit/tehtava.php?tunnus=mpera In English, it goes like this. Given a string S, find the longest string s such that s concatenated by itself is a substring of S. I was able to solve it by bash: echo hfhfggccaggccagccafff | grep -Po '(.*)\K\1' | awk 'length > l {l=length;s=$0} END{print s}' ggccaBut as I tried to solve in Python, I got an error: import subprocess s = 'hfhfggccaggccagccafff' command = "echo hfhfggccaggccagccafff | grep -Po '(.*)\K\1' | awk 'length > l {l=length;s=$0} END{print s}" #output = subprocess.check_output(command, shell=True) output = subprocess.run(command, stdout=subprocess.PIPE) print(output.stdout)FileNotFoundError: [Errno 2] No such file or directory: "echo hfhfggccaggccagccafff | grep -Po '(.*)\\K\x01' | awk 'length > l {l=length;s=$0} END{print s}" So in Python, how can I call a bash script and get the result string to a variable such that I can use it in Python? RE: How to call bash on Python and put the result to the variable? - mcmxl22 - May-28-2020 Try using os.system("echo hfhfggccaggccagccafff | grep -Po '(.*)\K\1' | awk 'length > l {l=length;s=$0} END{print s}") .You will need to import os .
RE: How to call bash on Python and put the result to the variable? - snippsat - May-28-2020 meknowsnothing Wrote:I was able to solve it by bash:A Python solution had been more readable Try this,see the extra K \ \1import subprocess command = subprocess.run("echo hfhfggccaggccagccafff | grep -Po '(.*)\K\\1' | awk 'length > l {l=length;s=$0} END{print s}'",shell=True, capture_output=True) print(command.stdout.decode().strip()) @mcmxl22 os.system() should not be used any more,subprocess replace it.subprocess subprocess Wrote:This module intends to replace several older modules and functions: RE: How to call bash on Python and put the result to the variable? - meknowsnothing - May-28-2020 I tried with some data from https://www.ohjelmointiputka.net/tiedostot/mpera.zip This is what I wrote import subprocess cases=['hfhfggccaggccagccafff', 'cdddccdcccccddcddddcdcccccdcccdcddcddddddddcdddcdccccdccdcddcccddcdddcccccdddcdcdcddddccdddddddddcccccdcccdccdcddddcddcccdcdddccdcdddccdcddccccddddccddcdddccccdcdcddcddccccccccddcddddcccccccddcccdcddccddcddcccdddccddcdddcdccdcddccccddcccdcccdccdddcddddcddddccddcdcdcddddcddcdcddcccddcccdcdddccdcddddcdddcdcccddddccdddcdcccccdcdccccddddccdddccddcccdddddddddccdcdcdcdcdddcdcccdccdcdcdcdddcdccddccddccddccdcccdcddcdccddcdccdddddcdddcddccccddccccccddcdccdcccddccddccdccdcdcdccdcccdcdddcddcdcdddccddcccddc', ... ] for i in range(len(cases)): s = cases[i]#'hfhfggccaggccagccafff' command = subprocess.run("echo "+s+" | grep -Po '(.*)\K\\1' | awk 'length > l {l=length;s=$0} END{print s}'",shell=True, capture_output=True) mjono = command.stdout.decode().strip()*2 #print(mjono) print(str(i+1)+" "+str(len(mjono)//2)+" " + str(s.find(mjono)+1))OSError: [Errno 7] Argument list too long: '/bin/sh' It looks like the original zip file has too big lines for this approach. RE: How to call bash on Python and put the result to the variable? - ndc85430 - May-30-2020 What's the point of using Python if you really want to write a shell script?! If you're going to use Python, implement a solution using its features, rather than getting it to run another process. |