Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - using subprocess to call sed?

I wish to call sed from python using subprocess. The script I tried using is below. however, this pipes the sed output to the standard terminal. It seems that the '>' operator is not recognised from within my subprocess.call statement. Any suggestions?

import sys 
import os 
import subprocess

files = os.listdir(sys.argv[1])

count = 0

for f in files:
    count += 1
    inp = sys.argv[1] + f
    outp = '../' + str(count) + '.txt'
    sub = subprocess.call(['sed', 's/\"//g', inp, '>', outp])

Also - my file names have spaces in them, i.e., " file1 .txt". Could this be the issue? My sed command works fine when I call sed from the terminal, just not from the script.

Thanks.

like image 625
Darren J. Fitzpatrick Avatar asked Jul 15 '11 12:07

Darren J. Fitzpatrick


1 Answers

It would be much faster to skip running all the sed processes and just do the work in Python

import os
import sys
files = os.listdir(sys.argv[1])

for count, f in enumerate(files):
    with open( os.path.join(sys.argv[1],f), "r" ) as source:
        with open( os.path.join('..',str(count)+'.txt'), "w" ) as target:
            data= source.read()
            changed= source.replace('"','')
            target.write( changed )

This will run considerably faster, since it won't fork a lot of subprocesses.

like image 157
S.Lott Avatar answered Sep 21 '22 12:09

S.Lott