Python-使用子进程调用sed?

Python-使用子进程调用sed?,python,sed,subprocess,Python,Sed,Subprocess,我希望使用subprocess从python调用sed。下面是我尝试使用的脚本。但是,这会将sed输出传输到标准终端。似乎无法从subprocess.call语句中识别'>'运算符。有什么建议吗 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

我希望使用subprocess从python调用sed。下面是我尝试使用的脚本。但是,这会将sed输出传输到标准终端。似乎无法从subprocess.call语句中识别'>'运算符。有什么建议吗

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])
此外,我的文件名中有空格,即“file1.txt”。这可能是问题所在吗?当我从终端调用sed时,我的sed命令可以正常工作,只是不能从脚本调用

谢谢。

使用

out_file = open(outp, "w")
sub = subprocess.call(['sed', 's/\"//g', inp], stdout=out_file )

跳过运行所有sed进程,只使用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 )

这将运行得更快,因为它不会产生很多子进程。

有什么理由不在Python中这样做吗?@robert+1这是一个很好的观点,你应该提供它,包括解决方案,作为一个答案。@robert,我现在没有喝过咖啡,但我看不到任何空间?这很有意义。谢谢D:-)我认为应该是
changed=data.replace(“,”)
,对吗?