2

I want to write a python script to format files using Seds.

sub = subprocess.call(['sed', 's/=/ = /g', infilename], stdout = outfile)
sub = subprocess.call(['sed', 's/=  = /==/g', infilename], stdout = outfile)

But this would write the entire changed infile twice to outfile (outfile contains two infiles in which each has one sed change). Is there a way to execute the two (and more) sed commands on the infile and write only once to the outfile which contains all the sed changes.

aa51
  • 75
  • 1
  • 9
  • 2
    What's the point of this? Why wouldn't you do it entirely in Python? You're not even using a regular expression, it's a simple text replace. – Daniel Roseman Oct 08 '15 at 15:22
  • @DanielRoseman I got your point. This is just a simple example. I want to execute multiple terminal commands in a python script. The commands could be different from seds and do other operations. But all these commands are expected to change the file and the change would only be saved once. – aa51 Oct 08 '15 at 15:25
  • chain commands with pipes, put in a script, execute the script? – yurib Oct 08 '15 at 15:30
  • @yurib That's a working idea. Is there a more pythonic way to do this? – aa51 Oct 08 '15 at 15:34
  • @ruyuan IMHO the most pythonic way would have the least non-python code in it (i.e. export commands to external script). However, looks like Chads answer is what you're looking for. – yurib Oct 08 '15 at 15:41
  • Thanks, @yurib. I was thinking if there was any way to put all the commands in a list and process them all with some python api. You are right that these operations are already well done in non-python ways. – aa51 Oct 08 '15 at 15:49

2 Answers2

3

You can chain them.

with open("out", "w") as outfile:
    with open("in", "r") as infile:
        first = subprocess.call([...], stdin=infile, stdout=subprocess.PIPE)
        second = subprocess.call([...], stdin=first.stdout, stdout=outfile)
        second.communicate()
Chad Miller
  • 1,435
  • 8
  • 11
  • (This is analogous to "sed outfile" .) – Chad Miller Oct 08 '15 at 15:35
  • You should close `first.stdout` after forking `second`. The last line should simply be `second.wait()`. There's no point in using the much more complex `communicate()` method here. – Sven Marnach Oct 08 '15 at 16:10
  • @ruyuan This is a good answer for showing how to work with pipes in python; shell makes working with pipes much simpler. – dsh Oct 08 '15 at 16:31
0

You can glue both seds in one using ; :

sub = subprocess.call(['sed', 's/=/ = /g;s/=  = /==/g', infilename], stdout = outfile)

They will go one after another.

midori
  • 4,807
  • 5
  • 34
  • 62