Python Forum
How to call bash on Python and put the result to the variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call bash on Python and put the result to the variable?
#1
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?
Reply
#2
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.
Reply
#3
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*
Reply
#4
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.
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Re-write BASH script to Python script popi75 5 2,446 Apr-30-2021, 03:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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