Python Forum

Full Version: How to call bash on Python and put the result to the variable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Could anyone help me to solve the question on https://www.ohjelmointiputka.net/postit/...nnus=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}'
ggcca
But 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?
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.
meknowsnothing Wrote:I was able to solve it by bash:
A Python solution had been more readable Wink

Try this,see the extra K\\1
import 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())
Output:
ggcca
@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:

os.system
os.spawn*
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.
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.