agatha
agatha

Reputation: 43

I have integrated a bash command into my Python code - how can I get this command to take an input from the shell?

I have added a bash command to my Python script - this bash command is used to linearise an input file (i.e. take a file with many lines, and merge it into one line). It works when I manually write the name of the file for the command (in this example, the input file is called input.txt):

import subprocess

holder = subprocess.Popen('perl -ne "chomp;print;" input.txt', shell=True, stdout=subprocess.PIPE).stdout.read()

print(holder)

However, I would like this bash command to take the name of the file specified by the user in the command line, and linearise it.

I have already tried to achieve this using %s:

import subprocess
import sys
import os

path = sys.argv[1]
file = open(path, 'r')
inp = file.read()

holder = subprocess.Popen('perl -ne "chomp;print;" %s' % inp, shell=True, stdout=subprocess.PIPE).stdout.read()

print(holder)

However, when I try this, the shell freezes and shows no output, and the bash $ prompt does not show, there is no error message.

I would like to know if there is a valid way to add a user input in this case and would appreciate any help.

EDIT: just to clarify, here is what I type into my shell to run the program with the second example:

$ python3 program.py input.txt

Upvotes: 0

Views: 99

Answers (2)

Corentin Limier
Corentin Limier

Reputation: 5006

import subprocess
import sys
import os

holder = subprocess.Popen('perl -ne "chomp;print;" {}'.format(sys.argv[1]), shell=True, stdout=subprocess.PIPE).stdout.read()

print(holder)

But why ? Why calling a perl script using python ? Python has all the tools you need to do the same using native, debuggable and crossplatform code on one process. It does not make sense and it is a bad practice.

import sys

with open(sys.argv[1]) as input_file:
    holder = ''.join(input_file.read().splitlines())

print(holder)

Upvotes: 2

Christian W.
Christian W.

Reputation: 2650

Why not just directly feed the input path to your subprocess, like this? I'm slightly confused as to why you would like to use perl within python do this however..

import subprocess
import sys
import os

path = sys.argv[1]

holder = subprocess.Popen('perl -ne "chomp;print;" %s' % path, shell=True, stdout=subprocess.PIPE).stdout.read()

print(holder)

with this example input:

test.txt
i
i
i
i
i

the program will output:

python linearize.py test.txt
b'iiiii'

Upvotes: 2

Related Questions