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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With