1

I am trying to call a process in linux bash using data stream signal '<' to use a file as input to an application.

However, this application do not receives its input from the file. I am using this:

#the application do not receives data stream from file

command = './grid < /home/felipe/Documents/proteins/grid.in'.split(' ')

p = subprocess.Popen(command,stdout=subprocess.PIPE)

But it does not work such as os.system(), that did what I want to do with subprocess:

#works
command = './grid < /home/felipe/Documents/proteins/grid.in'.split(' ')

os.system(command)

How can I use data stream signal '<' with subprocess module to get input to an application?

1 Answers1

1

If you just wanted to use Python as a shell wrapper, you would have to launch your (sub)process with shell=True. But since you're already using Python, you may want to rely on Python more and on shell less, you could do the following:

with open('/home/felipe/Documents/proteins/grid.in') as in_file:
    p = subprocess.Popen('./grid', stdin=in_file)

This opens your input file as in_file and feeds it into standard input of ./grid just like shell redirection would do.

Ondrej K.
  • 8,841
  • 11
  • 24
  • 39